我正在使用 rails 3.2.13,我有两个实体的模型,像这样
class Restaurant < ActiveRecord::Base
attr_accessible :description, :menu, :restaurant_name
has_many :cuisines
end
class Cuisine < ActiveRecord::Base
attr_accessible :cuisine_name, :restaurant_id
attr_accessible :cuisine_ids
belongs_to :restaurant
end
创建餐厅的控制器动作如下所示
我有一个使用像这样的简单表单 gem 创建餐厅的表单
<%= simple_form_for @restaurant do |f| %>
<%= f.input :restaurant_name %>
<%= f.input :description %>
<%= f.input :menu %>
<%= f.association :cuisines, label_method: :cuisine_name %>
<%= f.button :submit %>
<% end %>
我基本上假设从一组简单的形式有帮助的美食中选择。但是,当我选择美食并尝试创建餐厅时。它带回了错误。
ActiveModel::MassAssignmentSecurity::Error at /restaurants
Can't mass-assign protected attributes: cuisine_ids
正如您在模型中看到的那样。我将属性设置为可访问,但它不起作用。我什至尝试了单数版本的美食,但没有成功。我不知道出了什么问题?我宁愿不要篡改 rails 默认值以防止大规模分配。有什么线索吗?