Rails: Using form_for multiple times (DOM ids)

拥有回忆 提交于 2019-11-30 08:16:13

You can use :namespace => 'some_unique_prefix' option. In contrast to :index, this will not change the value used in the name attribute.

It's also possible to use an array, e.g. when you have nested forms or different forms that happen to have some fields in common: :namespace => [@product.id, tag.id] or :namespace => [:product, @product.id]

I found the answer myself, one can pass a :index option to form_for. That string will be used in the id and for attributes:

<%= form_for @person, :index => @person.id do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

will parse

<form accept-charset="UTF-8" action="/person/11" class="edit_person" id="edit_person_11" method="post">
  <!-- Hidden div for csrf removed -->
<label for="person_11_name">Name</label> 
<input id="person_11_name" name="person[11][name]" size="30" type="text" /> 
<input name="commit" type="submit" value="Update Person" /> 
</form>

Notice it'll change the name of the inputs as well.

I believe you can add this param:

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