0

I'm a little confused on how hidden form fields work in Rails, like for example I have two hidden fields in my form like so:

(for a polymorphic model for commenting)

<%= form_for [commentable, commentable.comments.build], :remote => 'true' do |form| %>
  <%= hidden_field "resource", commentable.class.to_s.downcase %>
  <%= hidden_field "resource_id", commentable.id %>

  <%= form.text_area :body %>
  <%= submit_tag 'Post comment' %>
<% end %>

But so this works fine and what not, but the params that I receive in my controller are like this:

"resource"=>"photos", "resource_id"=>{"174"=>""}

Why is my resource_id param a hash too?

2 Answers 2

3

<%= hidden_field "resource_id", commentable.id.keys[0] %>

A better question would be, where and how are you assigning the id?

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

1 Comment

hmmm... odd, this seems like the proper solution, I do however receive this error: undefined method keys' for 1:Fixnum`
0

This doesn't answer the question directly, but... Take out the resource_id and look in your params in the log file, I think the id is already passed in the url because of the path you provide in the form_for call

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.