ruby-on-railsruby-on-rails-4.1

data is not saved in database in rails


I am new rails and was creating a simple application where a user will can add posts and then add comments to that posts. the code for the model is

 # == Schema Information
#
# Table name: comments
#
#  id         :integer          not null, primary key
#  issue_id   :integer
#  content    :text
#  created_at :datetime
#  updated_at :datetime
#

class Comment < ActiveRecord::Base



    belongs_to :issue

end

the code for the controller is

class CommentsController < ApplicationController


def create 
    @issue = Issue.find(params[:issue_id])
    @comment = @issue.comments.create(comment_params)

    if @comment.save
        redirect_to :controller => 'issues', :action => 'index'
    else 
        render 'new'
    end
end 



def create
end 



private
    def comment_params
        params.require(:comment).permit(:content)

    end


end

the following association has ben added to issue controller

has_many :comments

when I enter the data in the form, the rails doesn't save the data to the database, instead it shows me the contents of the file comment.html.erb

Please help


Solution

  • When you declare methods in a Ruby class, the last definition of the method overrides any previous declaration that has the same method name.

    You have two create methods, one with logic and the second one empty. Because the second one is empty, it's being run instead of the first one, where all your logic is.