Sending data in my form hidden field

2019-06-05 03:13发布

问题:

I am trying to add a jquery upload But i need to pass a parameter into my form

Here the form

<%= form_for Photo.new do |f|%>
  <%= f.label :title %><br />
  <%= f.text_area :title, :rows => 3, :value => "Add a photo", :onclick=>"this.value='';" %>
  <%= f.file_field :photo, multiple: true %>
  <%= f.hidden_field('event_id', params[:event_id]) %>
  <%= f.submit "Add Picture" %>
<% end %>

But this doesn't work, the issue is I have a link has follow localhost/events/16/photos/useradd

where 16 is the event_id params. How can i place it into my hidden field so it is send afterwards?

UPDATE

NoMethodError in Photos#useradd
Showing /home/jean/rail/voix/app/views/photos/useradd.html.erb where line #13 raised:
undefined method `merge' for "16":String

that the error and 16 is my event_id

回答1:

Ok, shot in the dark here:

Try

<%= f.hidden_field 'event_id', :value => params[:event_id] %>

Edit - Why it worked:

The hidden_field method has the following signature: hidden_field(object_name, method, options = {}) See the docs about hidden_field

You have passed the value 16 as a method to be called by hidden_field, and that would not work.

The third argument of hidden_field is a options hash. Setting the "value" field of that hash will set the value of the input tag generated by the helper, and thus effectively setting '16' as the value of that form field. :)

Best regards