rails nested form assign unique id to generated field

佐手、 提交于 2019-12-10 20:01:06

问题


I want to assign unique Id to each row generated by nested form .

I looked this question Creating unique id for <fieldset> when using form_for Rails Nested Model Form but this doesn't solve my problem .


回答1:


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.

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.

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

For example: if you have

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>

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"




回答2:


I managed to solve this problem by looking into nested form gem . Inside nested gem unique Id is generated for each addition dynamically. data-blueprint is parsed and "new_#{association}" is replaced with generated unique ID.In my case relation was like 'Catalogues' has_many 'Category' so my association was categories. I assigned new row with id = "new_categories" and its replaced by unique Id when new row is added.

Maybe its useful some one else.



来源:https://stackoverflow.com/questions/15782006/rails-nested-form-assign-unique-id-to-generated-field

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