国境の南

Ruby, Railsメイン

fields_for_with_indexみたいなことをしたい時

こういう感じで出来る模様

<%= form_for @person do |person_form| %>
  ...
  <%= person_form.fields_for :projects do |project_fields| %>
    Project #<%= project_fields.index %>
    ...
  <% end %>
  ...
<% end %>

Note that fields_for will automatically generate a hidden field to store the ID of the record. There are circumstances where this hidden field is not needed and you can pass hidden_field_id: false to prevent fields_for from rendering it automatically.

参考 fields_for (ActionView::Helpers::FormHelper) - APIdock