我試圖創建我的狗,並添加類別,但我得到的錯誤:有很多:通過關聯:無法批量分配受保護的屬性:model_id?
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
對於希望在數據庫的時候,但是這可能會導致我有其他問題,但它仍然分配類別一些奇怪的原因。可能是什麼問題呢?
UPDATE
這裏是我的狗的形式:
<%= 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
如果增加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)
這是一個問題,如果它在技術上的工作?
當我這樣做,它說它不能批量分配'category_ids'。另一方面,如果我有兩個'category_ids/id',它會說它不能批量分配'category_ids'。到目前爲止,所有這三種方式都給我帶來了質量分配問題。我正在嘗試將多個類別分配給窗體上的Dog。我更新了我的問題以向您展示我的表單。 – LearningRoR 2012-07-29 18:27:47
現在,我承諾你的意思是...你想同時選擇多個類別......好吧,我認爲還有其他問題......我只是創建一個[應用程序](https://github.com/gabrielhilal/untitled)來測試它,並且所有內容都可以在Dog類的'attr_accessible:name,:category_ids'上運行良好... – gabrielhilal 2012-07-29 20:05:40