Hidden Field in Rails Form

Rails: How do I use hidden_field in a form_for?

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 controller

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

Note when you used

   <%= f.hidden_field :some_column %>

it change to html

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

and when you used

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

it change to html

   <input id="selected" name="selected" type="hidden" value="none"/>

rails simple_form - hidden field - create?

try this

= f.input :title, :as => :hidden, :input_html => { :value => "some value" }

How to use hidden field tag in rails

Try doing:

<%= f.hidden_field :client_id, value: @assessment.client_id %>

Since, as in your prior question, you are setting @assessment.client_id in your new action.

How do I put a default string value into a hidden field?

<%=f.hidden_field :role, :value => 'Reader', readonly: true%> or
<%=f.text_field :role, :value => 'Reader', readonly: true%>

By giving " readonly: true " the value could not be editable in the form

Rails Hidden Field Not Saving Params

@axel is right, you need to attach your hidden_field call to your form instance, f in this case. If you want the hidden inputs to build properly but not be a part of the milestone you should be using hidden_field_tag instead of hidden_field.

So either

    <%= f.hidden_field :area_id %>
<%= f.hidden_field :goal_id %>

or

    <%= hidden_field_tag :area_id, value: @goal.area_id %>
<%= hidden_field_tag :goal_id, value: @goal.id %>


Related Topics



Leave a reply



Submit