0

在选择标签中遇到问题,accepts_nested_attributes_for 具有许多显式外键。我无法获得列出的相关值。

Models

class PlantPlate < Plate
  has_many :unit_plates, :foreign_key => 'parent_id', :dependent => :destroy
  accepts_nested_attributes_for :unit_plates, :allow_destroy => true
end

class UnitPlate < Plate
  belongs_to :plant_plate, :foreign_key => 'parent_id'
end

View /plant_plates/_form.html.erb

<%= nested_form_for([ :admin, @plant_plate ]) do |f| %>
  <%= f.fields_for :unit_plates  do |unit_plate| %>
    <%= unit_plate.collection_select :parent_id, UnitPlate.all,:id,:name %>
<%end
<%end%>

我想在 select tag 中列出所有关联的单元板。但不知何故,现在能够用这个选择标签做到这一点。

提前致谢

4

2 回答 2

0

尝试使用基本的 f.select:

<%= f.select :parent_id, options_from_collection_for_select(UnitPlate.all, 'id', 'name') %>
于 2013-07-07T18:54:03.430 回答
0

试试form_for吧:

<%= form_for([:admin, @plant_plate]) do |f| %>
  <%= f.fields_for :unit_plate do |unit_plate| %>
    <%= unit_plate.collection_select :parent_id, UnitPlate.all, :id, :name %>
  <% end %>
<% end %>
于 2013-07-07T18:34:24.870 回答