我一直在試圖弄清楚爲什麼當我的代碼似乎是正確的時候,我總是收到質量分配錯誤。我使用錯誤的語法嗎?我真的很感激任何測試版,我一直在試圖調試我的應用程序的這樣一個簡單的部分,它真的停止了我的進度。我有以下幾點:rails 3質量分配錯誤,簡單卻無法解決
控制器
class ProductsController < ApplicationController
def new
@product =Product.new
end
def create
@product = Product.new(params[:product])
end
end
車型
class Product < ActiveRecord::Base
attr_accessible :name, :colors_attributes
has_many :colors
accepts_nested_attributes_for :colors, allow_destroy: true
end
class Color < ActiveRecord::Base
attr_accessible :name, :product_id
belongs_to :product
end
遷移
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.timestamps
end
end
end
class CreateColors < ActiveRecord::Migration
def change
create_table :colors do |t|
t.integer :product_id
t.string :name
t.timestamps
end
end
end
查看
產品/ new.html.erb
<%= render "form" %>
產品/ _form.html.erb
<%= form_for @product do |f| %>
<%= f.text_field :name %>
<%= f.fields_for :color do |c| %>
<%= render 'color_fields', f: c %>
<% end %>
<%= f.button :submit %>
<% end %>
產品/ _color_fields.html.erb
<%= f.text_field :name %>
錯誤:
ActiveModel::MassAssignmentSecurity::Error in ProductsController#create
Can't mass-assign protected attributes: color
完全跟蹤:
activemodel (3.2.3) lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
activemodel (3.2.3) lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
activemodel (3.2.3) lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
activemodel (3.2.3) lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
activerecord (3.2.3) lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
activerecord (3.2.3) lib/active_record/base.rb:498:in `initialize'
app/controllers/products_controller.rb:7:in `new'
app/controllers/products_controller.rb:7:in `create'
actionpack (3.2.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.3) lib/abstract_controller/base.rb:167:in `process_action'
...
任何見解將不勝感激
什麼是你得到的錯誤。 – David
我在文章的底部附加了錯誤和完整跟蹤 – godzilla3000