0
我通過has_many關聯有一個父對象(full_application)和一個子對象集合(fullapplication_districts)的嵌套表單。我試圖允許刪除窗體上的單個子對象(通過JavaScript),但要做到這一點,我需要能夠獲取視圖中的每個子對象的ID傳遞給控制器。 fields_for爲id創建一個隱藏的輸入字段,但我似乎無法弄清楚如何從中獲取id。在下面的例子中,記錄是呈現的子對象列表中的第13位。訪問fields_for hidden_field的Rails ID
<input type="hidden" value="538" name="full_application[fullapplication_districts_attributes][12][id]" id="full_application_fullapplication_districts_attributes_12_id">
這裏的視圖的形式設置:
<%= form_for(@full_application, url: full_applications_edit_path, method: :put) do |f| %>
<%= f.fields_for :fullapplication_districts do |fad| %>
<%= fad.collection_select :district_id, District.all, :id, :name, {include_blank: true}, {class: 'form-control'} %>
<%= fad.number_field :percent_one, class: 'form-control', step: :any %>
<%= fad.number_field :percent_two, class: 'form-control', step: :any %>
<%= fad.number_field :percent_three, class: 'form-control', step: :any %>
<%= link_to full_applications_districts_path(???), method: :delete, remote: true, data: { confirm: "Are you sure you want to delete this record?" } do %>
<i class="fa fa-trash"></i>
<% end %>
<% end %>
<% end %>
太棒了,非常感謝! – CChandler81
不客氣:) – fongfan999