Passing NULL when it shouldn't

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” %>

<%= submit_tag “Merk op!” %>

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?

On Thu, Apr 7, 2011 at 11:12 AM, Kelly P. [email protected]
wrote:

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.

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

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” %>

<%= submit_tag “Merk op!” %>

Now i am getting a TypeError ‘can’t convert Symbol into String’ from
that new line though.

On 8 April 2011 11:26, Kelly P. [email protected] wrote:

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

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

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs