accepted_nested_attributes具有嵌套形式和选择字段

时间:2013-07-07 18:13:32

标签: ruby-on-rails ruby ruby-on-rails-3

面向具有accepts_nested_attributes_for的select标记中的问题,具有许多显式外键。我无法获得列出的关联值。

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

我想在选择标记中列出所有关联的单位面板。但不知怎的,现在能够用这个选择标签做到这一点。

提前致谢

2 个答案:

答案 0 :(得分: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 %>

答案 1 :(得分:0)

尝试使用基本的f.select:

<%= f.select :parent_id, options_from_collection_for_select(UnitPlate.all, 'id', 'name') %>