Friday, 15 August 2014

Ruby on Rails: Create doesn't work. Data doesn't insert to database -



Ruby on Rails: Create doesn't work. Data doesn't insert to database -

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 comment: </h2> <%= form_for([@record, @record.comments.build]) |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 commentscontroller#create html i, [2014-06-26t14:13:49.802236 #20539] info -- : processing commentscontroller#create 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".* "records" "records"."id" = ? limit 1 [["id", 4]] d, [2014-06-26t14:13:49.807138 #20539] debug -- : record load (0.1ms) select "records".* "records" "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 http://0.0.0.0:3000/records/4 i, [2014-06-26t14:13:49.822714 #20539] info -- : redirected http://0.0.0.0:3000/records/4

when create comment, info doesn't write database suggestion? way, why each logger info has double output?

try adding accepts_nested_attributes_for :comments record model

ruby-on-rails ruby

No comments:

Post a Comment