How do I use hidden_field in a form_for in Ruby on Rails?

Deadly 提交于 2019-11-28 16:37:16

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"/>

You can send a custom value as a hidden input for your model like that:

<%= f.hidden_field :version, value: 12 %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!