rails嵌套表单将唯一的id分配给生成的字段 [英] rails nested form assign unique id to generated field
问题描述
我想为嵌套表单生成的每一行分配唯一的ID.
I want to assign unique Id to each row generated by nested form .
我看了这个问题为< fieldset>使用form_for Rails嵌套模型表单时,但这不能解决我的问题.
I looked this question Creating unique id for <fieldset> when using form_for Rails Nested Model Form but this doesn't solve my problem .
推荐答案
实际上,Anil Maurya提供的解决方案仅适用于使用gem提供的link_to_add
通过javascript动态添加的项目.但是对我来说,它将new_association
留给已经存在的项目.
Actually, the solution given by Anil Maurya only works for items that have been dynamically added via javascript using link_to_add
provided by the gem. But for me, it would leave new_association
for already existing items.
为了正确地标识每个嵌套元素(甚至是深度嵌套的元素),可以使用f.index
.优点是,如果该条目已经存在(意味着,如果它已经存在于数据库中),那么它将被数组中具有的索引替换.但是,使用link_to_add
会将f.index
替换为nested_form自动生成的ID,就像Anil Maurya的解决方案一样.
In order to properly identify every nested element (even the deeply nested ones), it is possible to use f.index
. The advantage is that if the entry already exists (meaning, if it was in the database), then it will just be replaced by the index that it has in the array. However, using link_to_add
will replace f.index
by the ID automatically generated by nested_form, just like Anil Maurya's solution.
对于深度嵌套的解决方案,而不是每次都编写fields_for
并使用相同的变量f
,如果需要正确标记元素,则应指定要渲染的部分,并每次使用不同的f
变量,您应该将其传递给部分变量
For deeply nested solutions, instead of just writing fields_for
and using the same variable f
everytime, if you need to properly tag your elements, your should specify which partial to render and each time use a different f
variable, which you should pass to the partial
例如:如果您有
Main has_many :nested1s
Nested1 has_many :nested2s
Nested2 has_many :nested3s
然后您的代码应如下所示(我举了一个表的示例,因为它比较困难):
Then your code should look like this (I gave an example with tables because it's more difficult):
<%= nested_form_for @Main do |main_form| %>
<table>
<thead>
<stuff />
</thead>
<%= main_form.fields_for :nested1s, :wrapper => false do |nested1_f| %>
<%= render 'main/nested1_fields', nested1_f: nested1_f %>
<% end %>
_nested1_fields.html.erb
<tbody class="fields">
<tr>Stuff</tr>
<tr>
<table id="nested1-<%= nested1_f.index %>-nested2s">
<thead>Stuff</thead>
<%= nested1_f.fields_for :nested2s, :wrapper => false do |nested2_f| %>
<%= render 'main/nested2_fields', nested1_f: nested1_f, nested2_f: nested2_f %>
<% end %>
</table>
</tr>
<tr><%= nested1_f.link_to_add 'add nested2', :nested2, :data => { :target => "#nested1-#{nested1_f.index}-nested2s"}, class: "btn btn-sm btn-success" %>
</tbody>
_nested2_fields.html.erb
<tbody class="fields">
...
<table id="nested1-<%= nested1_f.index %>-nested2-<%= nested2_f.index %>-nested3s">
<%= fields_for :nested3s do |nested3_f| %>
...
...
</tbody>
还必须注意,gem nested_form仅在每个嵌套字段集都被带有"fields"类的标签包裹的情况下才能正常工作
Also it is important to note that the gem nested_form will only work well if every nested fieldsets are wrapped by a tag with the class "fields"
这篇关于rails嵌套表单将唯一的id分配给生成的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!