Rails访问fields_for hidden_​​field id

时间:2017-04-01 17:15:31

标签: ruby-on-rails nested-forms fields-for

我有一个带有父对象(full_application)的嵌套表单和一个通过has_many关联的子对象集合(fullapplication_districts)。我试图允许删除表单上的单个子对象(通过javascript),但为此我需要能够获取视图中每个子对象的id以传递给控制器​​。 fields_for为id创建一个隐藏的输入字段,但我似乎无法弄清楚如何从中获取id。在下面的示例中,记录是渲染子对象列表中的第13个。

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

1 个答案:

答案 0 :(得分:1)

您可以使用: #+CAPTION: Weight space #+ATTR_HTML: :alt neural network :title Neural network representation :align right #+ATTR_HTML: :width 50% :height 50% https://i.stack.imgur.com/nzHSl.jpg fad.object 。这将返回 fullapplication_district 实例。

fad.object.id