我正在使用 Rails 3.2 和 Ruby 1.9.3。
我知道这已经被一次又一次地问过,但我找不到任何关于我的特殊情况的东西,即使这肯定是非常愚蠢的事情,我也无法弄清楚。
我的关系是这样的:一个产品有很多类别,一个类别有很多属性。
我想在我的视图中创建一个嵌套表单,所以我正在使用
= f.fields_for :categories do |category|
= render 'category_fields', :f => category
为了将类别字段“附加”到产品表单。
问题是,当它转换为 HTML 时,类别输入的名称是“categories_attributes”,如下所示:
<label for="product_categories_attributes_0_name">Name</label>
<input id="product_categories_attributes_0_name" type="text" size="30" name="product[categories_attributes][0][name]">
我是 Rails 新手,但我想它应该是product[categories][0][name]
而不是categories_attributes
.
提交表格后,我得到
Can't mass-assign protected attributes: categories_attributes
另外,我的模型:
class Product < ActiveRecord::Base
belongs_to :company
belongs_to :product_type
has_many :categories, :dependent => :destroy
accepts_nested_attributes_for :categories
attr_accessible :comments, :name, :price
end
class Category < ActiveRecord::Base
belongs_to :product
has_many :attributes, :dependent => :destroy
attr_accessible :name
accepts_nested_attributes_for :attributes
end
class Attribute < ActiveRecord::Base
belongs_to :category
attr_accessible :name, :value
end
我绝对确定这只是一个小错误,但我无法发现它。
帮助?