简体   繁体   English

Rails访问fields_for hidden_​​field id

[英]Rails accessing fields_for hidden_field id

I have a nested form with a parent object (full_application) and a collection of child objects (fullapplication_districts) via a has_many association. 我有一个嵌套的表单,其中包含一个父对象(full_application)和一个通过has_many关联的子对象集合(fullapplication_districts)。 I am attempting to allow for deletion of individual child objects on the form (via javascript) but to do so I need to be able to grab the id of each child object in the view for passing to the controller. 我试图允许删除表单上的各个子对象(通过javascript),但为此,我需要能够获取视图中每个子对象的ID,以传递给控制器​​。 fields_for creates a hidden input field for the id but I can't seem to figure out how to grab the id from it. fields_for为ID创建了一个隐藏的输入字段,但我似乎无法弄清楚如何从中获取ID。 In the example below the record is the 13th in the list of rendered child objects. 在下面的示例中,记录是已渲染子对象列表中的第13个。

<input type="hidden" value="538" name="full_application[fullapplication_districts_attributes][12][id]" id="full_application_fullapplication_districts_attributes_12_id">

Here's the form setup in the view: 这是视图中的表单设置:

<%= 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 %>

You can use: fad.object or fad.object.id . 您可以使用: fad.objectfad.object.id This will return to fullapplication_district instance. 这将返回到fullapplication_district实例。

 <%= link_to full_applications_districts_path(fad.object), method: :delete, remote: true, data: { confirm: "Are you sure you want to delete this record?" } do %>
    <i class="fa fa-trash"></i>
 <% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM