0

Two simple model:

Record

class Record < ActiveRecord::Base
    has_many :comments
    validates :title, presence: true,
                            length: { minimum: 5 }
end

Comment

class Comment < ActiveRecord::Base
  belongs_to :record
end

in viewer

<h2>Add a comment: </h2>
<%= form_for([@record, @record.comments.build]) do |f| %>
    <p>
       <%= f.label :commenter %><br>
    <%= f.text_field :commenter %>
    </p>
    <p>
        <%= f.label :body %><br>
        <%= f.text_area :body %>
    </p>
    <p>
        <%= f.submit %>
    </p>
<% end %>

in controller

def create
    @record = Record.find(params[:record_id])
    @comment = @record.comments.create(comment_params)
    logger.debug "comment check: #{@comment.attributes.inspect}"
    redirect_to record_path(@record)
end

Logger information:

I, [2014-06-26T14:13:49.802139 #20539]  INFO -- : Processing by CommentsController#create as HTML
I, [2014-06-26T14:13:49.802236 #20539]  INFO -- : Processing by CommentsController#create as HTML
I, [2014-06-26T14:13:49.802300 #20539]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"12u1/KgN8baCy8Jn/TJi3Ux7m258FZPfraHGUhl9WnM=", "comment"=>{"commenter"=>"qwe", "body"=>"asdsa"}, "commit"=>"Create Comment", "record_id"=>"4"}
I, [2014-06-26T14:13:49.802338 #20539]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"12u1/KgN8baCy8Jn/TJi3Ux7m258FZPfraHGUhl9WnM=", "comment"=>{"commenter"=>"qwe", "body"=>"asdsa"}, "commit"=>"Create Comment", "record_id"=>"4"}
D, [2014-06-26T14:13:49.807062 #20539] DEBUG -- :   Record Load (0.1ms)  SELECT  "records".* FROM "records"  WHERE "records"."id" = ? LIMIT 1  [["id", 4]]
D, [2014-06-26T14:13:49.807138 #20539] DEBUG -- :   Record Load (0.1ms)  SELECT  "records".* FROM "records"  WHERE "records"."id" = ? LIMIT 1  [["id", 4]]
D, [2014-06-26T14:13:49.810514 #20539] DEBUG -- :    (0.1ms)  begin transaction
D, [2014-06-26T14:13:49.810599 #20539] DEBUG -- :    (0.1ms)  begin transaction
D, [2014-06-26T14:13:49.821927 #20539] DEBUG -- :    (0.1ms)  commit transaction
D, [2014-06-26T14:13:49.822023 #20539] DEBUG -- :    (0.1ms)  commit transaction
D, [2014-06-26T14:13:49.822159 #20539] DEBUG -- : comment check: {"id"=>nil, "commenter"=>"qwe", "body"=>"asdsa", "record_id"=>nil, "created_at"=>nil, "updated_at"=>nil}
D, [2014-06-26T14:13:49.822201 #20539] DEBUG -- : comment check: {"id"=>nil, "commenter"=>"qwe", "body"=>"asdsa", "record_id"=>nil, "created_at"=>nil, "updated_at"=>nil}
I, [2014-06-26T14:13:49.822667 #20539]  INFO -- : Redirected to http://0.0.0.0:3000/records/4
I, [2014-06-26T14:13:49.822714 #20539]  INFO -- : Redirected to http://0.0.0.0:3000/records/4

When I create comment, data doesn't write to database any suggestion? By the way, why each logger information has double output?

1
  • So I've been screwing around with this particular problem and I encountered something strange. When attempting to create a Comment by itself (Comment.create) it actually creates a new Record instead. I had to remove the belongs_to :record association to prevent that from happening and I have no idea why it isn't working right now. Very very odd. Commented Jun 26, 2014 at 20:58

3 Answers 3

2

Try adding accepts_nested_attributes_for :comments to your Record model

Sign up to request clarification or add additional context in comments.

1 Comment

also, which controller is it that is actually doing processing? The form/logic you have looks like its for the records_controller but the logger says comments_controller
1

Have you captured anything in the variable comment_params? Don't you need to do something like comment_params = params[:comment] or something like that? I think comment_params is nil when you drop it into your create method (although I may be wrong).

def create
  @record = Record.find(params[:record_id])
  @comment = @record.comments.create(params[:comment])

  redirect_to record_path(@record)
end

Comments

0

In your controller, change comment.create to comment.build, so that the relationship gets created. Also, pass only the params for the comment:

def create
  @record = Record.find(params[:record_id])
  @comment = @record.comments.build(params[:comment])
  logger.debug "comment check: #{@comment.attributes.inspect}"
  redirect_to record_path(@record) 
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.