【发布时间】:2012-10-20 11:29:52
【问题描述】:
目前,Item belongs_to Company 和 has_many ItemVariants。
我正在尝试使用嵌套 fields_for 通过 Item 表单添加 ItemVariant 字段,但是使用 :item_variants 不会显示表单。只有当我使用单数时才会显示。
我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?
提前致谢。
注意:下面的 sn-ps 中省略了不相关的代码。
编辑:不知道这是否相关,但我正在使用 CanCan 进行身份验证。
routes.rb
resources :companies do
resources :items
end
item.rb
class Item < ActiveRecord::Base
attr_accessible :name, :sku, :item_type, :comments, :item_variants_attributes
# Associations
#-----------------------------------------------------------------------
belongs_to :company
belongs_to :item_type
has_many :item_variants
accepts_nested_attributes_for :item_variants, allow_destroy: true
end
item_variant.rb
class ItemVariant < ActiveRecord::Base
attr_accessible :item_id, :location_id
# Associations
#-----------------------------------------------------------------------
belongs_to :item
end
item/new.html.erb
<%= form_for [@company, @item] do |f| %>
...
...
<%= f.fields_for :item_variants do |builder| %>
<fieldset>
<%= builder.label :location_id %>
<%= builder.collection_select :location_id, @company.locations.order(:name), :id, :name, :include_blank => true %>
</fieldset>
<% end %>
...
...
<% end %>
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 nested-attributes fields-for