11155
(-- --)
April 7, 2011, 4:12pm
1
I'm having a problem passing along all parameters to post a comment:
<%= form_tag :action => "comment", :id => @zwemmer , :created_on =>
Time.now, :created_by => "number1" %>
<%= text_area "comment", "body" ,:size =>"25x10" %><br/>
<%= submit_tag "Merk op!" %>
</form>
In the console i get:
←[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?
Can you post your controller code that the form is submitting to? I’m curious to see how you are processing the values being sent over.
B.
11155
(-- --)
April 8, 2011, 9:18am
3
In ZwemmersController:
def comment
Zwemmer.find(params[:id]).comments.create(params[:comment])
flash[:notice] = "Added new comment."
redirect_to :action => "show", :id => params[:id]
end
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
11155
(-- --)
April 8, 2011, 10:26am
5
Thanks for the fast reply, i added your code:
<%= form_tag :action => "comment", :id => @zwemmer , :created_on =>
Time.now %>
<%= hidden_field :comment, :created_by, 'number1' %>
<%= text_area "comment", "body" ,:size =>"25x10" %><br/>
<%= submit_tag "Merk op!" %>
</form>
Now i am getting a TypeError 'can't convert Symbol into String' from
that new line though.
Sorry, I always forget which bits of Rails take symbols versus
strings. Try changing the parameters to hidden_field from symbols:
:comment, :created_by
to strings:
'comment', 'created_by'
i.e. the same way you have it for your call to text_area on the next line.
Chris
11155
(-- --)
April 8, 2011, 11:24am
7
No prob i found it via google, should be through :value:
<%= hidden_field :comment, :created_by, :value => "number1" %>
Everything working now, thanks much for the help!