How to use hidden_field in form_for in Ruby on Rails?

I read this one , but I'm new to RoR, so it's hard for me to understand it. I use the form to create a new request record, and all the variables that I need to submit already exist. Here is the data I need to send (this is in the do loop):

:user_id => w[:requesteeID] :requesteeName => current_user.name :requesteeEmail => current_user.email :info => e 

Here is my form that still works, but only sends NULL values ​​for everything:

 <% form_for(:request, :url => requests_path) do |f| %> <div class="actions"> <%= f.submit e %> </div> <% end %> 

How to use hidden_fields to send data that I already have? Thanks for reading.

+67
ruby-on-rails forms hidden-field
Jun 28 2018-10-10T00:
source share
2 answers

Ref hidden_field or hidden_field_tag

 <% form_for(:request, :url => requests_path) do |f| %> <div class="actions"> <%= f.hidden_field :some_column %> <%= hidden_field_tag 'selected', 'none' %> <%= f.submit e %> </div> <% end %> 

then in the controller

  params[:selected]="none" params[:request][:some_column] = request.some_column 

Pay attention when you used

  <%= f.hidden_field :some_column %> 

it will change to html

 <input type="hidden" id="request_some_column" name="request[some_column]" value="#{@request.some_column}" /> 

and when did you use

 <%= hidden_field_tag 'selected', 'none' %> 

it will change to html

  <input id="selected" name="selected" type="hidden" value="none"/> 
+88
Jun 28 '10 at 11:41
source share

You can send a custom value as hidden input for your model, for example:

 <%= f.hidden_field :version, value: 12 %> 
+55
Jul 16 '15 at 13:55
source share



All Articles