On 7 April 2011 17:12, Kelly P. [email protected] wrote:
←[4;36;1mComment Create (16.0ms)←[0m ←[0;1mINSERT INTO “comments”
(“created_
on”, “updated_at”, “body”, “zwemmer_id”, “created_by”, “created_at”)
VALUES(‘201
1-04-07 18:06:56’, ‘2011-04-07 18:06:56’, ‘testcontent’, 124, NULL,
‘2011-04-07
18:06:56’)
So to created_by it’s passing NULL instead of “number1”. How is this
possible?
If you look a little higher in your log, you’ll see the parameters
that are getting passed in the POST request. Judging from your form,
they’ll look something like this:
:comment => {:body => “testcontent”},
:created_by => 'number1
The problem is that you probably want the :created_by key to be inside
the :comment hash, not as a separate parameter. Otherwise, you’ll have
to manually set the ‘created_by’ attribute in your controller code,
e.g.:
@comment = Zwemmer.find(params[:id]).comments.build(params[:comment])
@comment.created_by = params[:created_by]
@comment.save
To avoid this, you want to make that ‘created_by’ parameter part of
the ‘comment’ hash. One way to do this would be to pass it as a hidden
field inside the form, rather than trying to add it to the form’s
action URL:
<%= hidden_field :comment, :created_by, ‘number1’ %>
so that your parameters would look like this:
:comment => {:body => ‘testcontent’, :created_by => ‘number1’}
and your controller code could be simply:
Zwemmer.find(params[:id]).comments.create(params[:comment])
Chris