我正在尝试创建我的 Dog 并向其添加类别,但出现错误:
WARNING: Can't mass-assign protected attributes: category_id
SQL (49.0ms) INSERT INTO "categorizations" ("dog_id", "category_id", "created_at", "cat_id", "mouse_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["dog_id", 7], ["category_id", 1], ["created_at", Sun, 29 Jul 2012 20:34:27 JST +09:00], ["cat_id", nil], ["mouse_id", nil], ["updated_at", Sun, 29 Jul 2012 20:34:27 JST +09:00]]
(105.0ms) COMMIT
Completed 500 Internal Server Error in 669ms
ArgumentError (too few arguments):
app/controllers/dogs_controller.rb:7:in `format'
app/controllers/dogs_controller.rb:7:in `create'
不知道为什么这是真的,因为我的模型中有:
class Dog < ActiveRecord::Base
attr_accessible :name, :category_ids
belongs_to :user
has_many :categorizations
has_many :categories, :through => :categorizations
accepts_nested_attributes_for :categories
validates :name, :presence => true
validates :user_id, :presence => true
end
如您所见,连接表是Categorizations
:
class Categorization < ActiveRecord::Base
belongs_to :dog
belongs_to :category
belongs_to :cat
belongs_to :mouse
end
我在表格上选择所需的类别。这是模型:
class Category < ActiveRecord::Base
attr_accessible :name
belongs_to :user
has_many :categorizations
has_many :dogs, :through => :categorizations
has_many :cats, :through => :categorizations
has_many :mouses, :through => :categorizations
validates :name, :presence => true, :uniqueness => true
before_validation :downcase_name
private
def downcase_name
self.name = self.name.downcase if self.name.present?
end
end
出于某种奇怪的原因,它在查看数据库时仍然会分配类别,但这可能会导致我遇到的其他问题。可能是什么问题呢?
更新
这是我的狗表格:
<%= form_for(@dog, :remote => true) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<%= f.label :category, "Categories" %>
<%= f.select :category_ids, Category.all.collect {|c| [c.name, c.id]}, {}, { :multiple => true, } %>
<% end %>
当我尝试时仍然得到质量分配错误:
attr_accessible :name, :category_ids
WARNING: Can't mass-assign protected attributes: category_id
attr_accessible :name, :category_id, :category_ids
# It says this for each category I want to put on the Dog.
WARNING: Can't mass-assign protected attributes: category_id
attr_accessible :name, :category_id
WARNING: Can't mass-assign protected attributes: category_ids
更新 2
respond_to
通过添加控制器操作,我能够摆脱 ArgumentError :
def create
@dog = current_user.dogs.new(params[:dog])
respond_to do |format|
if @dog.save
format.js
else
format.js
end
end
end
如果向 Dog 添加 2 个或更多类别(在本例中为 3)。它将它们添加到数据库中并进行批量分配,但服务器一直在日志中这样说:
Started POST "/dogs" for 127.0.0.1 at 2012-07-29 20:44:15 -0400
Processing by DogsController#create as JS
Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"wuPqA6e8MqF/yW3VQ+sfLiyf0olOWgEpnVC2qawQE0I=", "dog"
Add Dog"}
User Load (2.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Category Load (0.0ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 2, 3)
(1.0ms) BEGIN
(0.0ms) COMMIT
(0.0ms) BEGIN
(1.0ms) SELECT 1 FROM "categories" WHERE ("categories"."name" = 'furry' AND "categories"."id" != 1) LI
(1.0ms) SELECT 1 FROM "categories" WHERE ("categories"."name" = 'house trained' AND "categories"."id" != 2) LIMIT
(0.0ms) SELECT 1 FROM "categories" WHERE ("categories"."name" = 'wild' AND "categories"."id" != 3) LIMIT
SQL (1.0ms) INSERT INTO "dogs" ("created_at", "name", "updated_at", "user_id") VALUES ($1, $2, $3, $4
"name", "Omni Dog"], ["updated_at", Mon, 30 Jul 2012 09:44:15 JST +09:00], ["user_id", 1]]
WARNING: Can't mass-assign protected attributes: category_id
[1m[35mSQL (1.0ms)[0m INSERT INTO "categorizations" ("dog_id", "category_id", "created_at", "cat_id", "mouse_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["dog_id", 31], ["category_id", 1], ["created_at", Mon, 30 Jul 2012 09:44:15 JST +09:00], ["cat__id", nil], ["mouse_id", nil], ["updated_at", Mon, 30 Jul 2012 09:44:15 JST +09:00]]
WARNING: Can't mass-assign protected attributes: category_id
[1m[36mSQL (1.0ms)[0m [1mINSERT INTO "categorizations" ("dog_id", "category_id", "created_at", "cat__id", "mouse_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"[0m [["dog_id", 31], ["category_id", 2], ["created_at", Mon, 30 Jul 2012 09:44:16 JST +09:00], ["cat__id", nil], ["mouse_id", nil], ["updated_at", Mon, 30 Jul 2012 09:44:16 JST +09:00]]
WARNING: Can't mass-assign protected attributes: category_id
[1m[35mSQL (1.0ms)[0m INSERT INTO "categorizations" ("dog_id", "category_id", "created_at", "cat__id", "mouse_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["dog_id", 31], ["category_id", 3], ["created_at", Mon, 30 Jul 2012 09:44:16 JST +09:00], ["cat__id", nil], ["mouse_id", nil], ["updated_at", Mon, 30 Jul 2012 09:44:16 JST +09:00]]
Rendered dogs/_dog.html.erb (1.0ms)
Rendered dogs/create.js.erb (3.0ms)
Completed 200 OK in 203ms (Views: 25.0ms | ActiveRecord: 52.0ms)
如果它在技术上有效,这是一个问题吗?