"this.value='';"" name="description" /> "this.value='';"" />
13195

Sending data in my form hidden field

Question:

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

Answer1:

Ok, shot in the dark here:

Try

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

<strong>Edit - Why it worked:</strong>

The hidden_field method has the following signature: hidden_field(object_name, method, options = {}) <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-hidden_field" rel="nofollow">See the docs about hidden_field</a>

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

Recommend

  • Assign relation id to record on create, but not on update
  • NoMethodError within nested model form
  • How to identify hash elements in a loop when using jquery
  • how to use in and order by at the same time in cassandra
  • How to pass attribute data by clicking on image?
  • Unable to add create reminder for Calendar event on Marshmallow Device
  • Rename image using associated model - Paperclip
  • SQL query to exclude records that appear in other rows?
  • write text on image and show it to a imageview
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • How to change placeholder text in an autocomplete activity of android google place?
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • android Navigation Bar hiding and persantage of usable screen overlap
  • Button click event not firing in jQuery
  • Webgrid not refreshing after delete MVC
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • Rails Find when some params will be blank
  • JSON response opens as a file, but I can't access it with JavaScript
  • Jquery UI tool tip close icon
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Display issues when we change from one jquery mobile page to another in firefox
  • Android screen density dpi vs ppi
  • Different response to non-authenticated users and AJAX calls
  • req.body is undefined - nodejs
  • Bug in WPF DataGrid
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Traverse Array and Display in markup
  • Data Validation Drop Down Box Arrow Disappearing
  • Comma separated Values
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Qt: Run a script BEFORE make
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?