0

I use the nested_form_fields gem to tranlsate some specific fields in a technical document. Translations are nested fields associated to the document, so that each document has one instance of translation per field per language.

In the _form view to edit the document, nested_form_fields is called for each translated field, and the location to where display the corresponding input is set by the id of the contaning DIV:

<div class="row">
        <div class="col-md-1">
          <%= image_tag("next32.png", :id => "unfold") %>
        </div>
        <div class="col-md-1 text-right"> <%= t('Name')%>:
        </div>
        <div class="col-md-10 field"><%= f.text_field :name, :class => "col-md-8" %>
        </div>
      </div>
      <!-- Translations management -->
      <div class="translation">
        <div class="row">
          <div class="col-md-10 col-md-offset-2" id="name_translation">
            <%= f.nested_fields_for :translations  do |locution| %>
            <div class="row">
              <div class="col-md-1">
                <%= locution.collection_select :language, @other_languages, :property, :name  %>
              </div>
              <div class="col-md-8">
                <%= locution.text_field :description, :class => "col-md-10" %>
              </div>
              <div class="col-md-1">
                <%= locution.remove_nested_fields_link {image_tag("remove.png")} %>
              </div>
              <div class="col-md-1">
                <%= locution.hidden_field :field_name, :value => 'name' %>
              </div>
            </div>
            <% end %>
          </div>
        </div>
        <div class="row">
          <div class="col-md-10 col-md-offset-2">
            <%= f.add_nested_fields_link :translations, image_tag("add.png"), data: {insert_into: "name_translation"} %> <%= t('New') %>
          </div>
        </div>
        <br/>
      </div>
      <!-- End of translations -->

Using this code for 2 or 3 fields in a page, all the translated fields available to the page are displayed in each instance of the nested_fields_for method.

I was wandering if there is a way to add a filter in the f.nested_fields_for :translations (in the form of a block?), or if I have to take all the translation records and filter out in the do |locution| loop.

Thanks for your help!

1 Answer 1

0

The solution came from Nico Ritche, the author of the nested_form_fields gem himself:

Filtering does just work like with normal fields_for, you can supply the filtered translations directly:

f.nested_fields_for :translations, your_filtered_translations do |locution|

Which leads to the following update in my code:

<%= f.nested_fields_for :translations, @my_object.translations.where("field_name='name'") do |locution| %>

Thanks a lot Nico!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.