深度嵌套的表单和对象属性

Deeply nested form and object attributes

class 设备 has_many :equipment_maintenancesaccepts_nested_attributes_for : equipment_maintenances, allow_destroy: true

class 设备维护 has_many :equipment_maintenanceitemsaccepts_nested_attributes_for : equipment_maintenanceitems, allow_destroy: true

下面的形式可以正确生成 HTML 来更新孙子(即 name="equipment[equipment_maintenance_attributes][0][equipment_maintenanceitems_attributes][1][active]" 如下:

<%= form_for @equipments do |equipment| %>
  <% @equipment_maintenances.each do |equipment_maintenance| %>
    <%= equipment.fields_for :equipment_maintenance do |em| %>
      <%= em.fields_for :equipment_maintenanceitems do |f| %>
         <%= f.check_box :active %>
         <%= f.text_field :order %>
         <%# equipment_maintenanceitem.name  %>
         <%= f.select :frequenc, [3,4,6,12] %>
      <% end %>
    <% end %>
  <% end %>
  <%= equipment.submit %>
<% end %>

但是,我还没有找到引用孙子名字的正确语法equipment_maintenanceitem.name

更新 虽然正确回答了原始问题,但问题陈述不完整。 EquipmentMaintenanceItem belongs_to :ordinary_maintenanceitem 目标是获得 equipment_maintenanceitem.ordinary_maintenanceitem.name

<%= form_for @equipments do |equipment| %>
  <% @equipment_maintenances.each do |equipment_maintenance| %>
    <%= equipment.fields_for :equipment_maintenance do |em| %>
      <%= em.fields_for :equipment_maintenanceitems do |f| %>
         <%= f.check_box :active %>
         <%= f.text_field :order %>
         <%= f.label :name  %>
         <%= f.select :frequenc, [3,4,6,12] %>
      <% end %>
    <% end %>
  <% end %>
  <%= equipment.submit %>
<% end %>

一个简单的方法是使用标签来显示 equipment_maintenanceitems

根据问题的原始表述,答案是:

<%= f.object.name %>

根据更新的上下文,答案是:

<%= OrdinaryMaintenanceItem('id = ?',f.object.maintenanceitem_id).pluck("name") %>

效率不高,需要清理数组和字符串符号...