Skip to content

How to use nested_fields with auto increment number #101

Description

@imrasp

I would like to use nested_fields with auto increment number in a hidden field
currently, my code is

<%= f.nested_fields_for(:gate_directions) do |p| %>
  <div class="row" style="margin-top:15px;">
    <div class="col-sm-4">
      <%= p.text_field :name, class: "form-control" %>
    </div>
    <div class="col-sm-3">
      <%= p.hidden_field :idx %>
    </div>
    <div class="col-sm-1" style="margin-top: 5px;font-size: 18px;">
      <%= p.remove_nested_fields_link '<i class="fa fa-trash"></i>'.html_safe%>
    </div>
</div>
<% end %>
<%= f.add_nested_fields_link :products, "<i class='fa fa-plus'></i> product".html_safe%>

I want idx = 1 when first field created then 2 for the second one and so on. I have no idea to achieve this requirement, could you please give me some example or suggestion to implement this.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions