有很多:通过关联:无法批量分配受保护的属性:model_id?

时间:2012-07-29 12:01:06

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1

我正在尝试创建我的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

如果向狗添加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)

如果它在技术上有效,这是一个问题吗?

2 个答案:

答案 0 :(得分:1)

这里有一个额外的s

class Dog < ActiveRecord::Base
  attr_accessible :name, :category_ids
  ...

category_iddogs表格中的一列,因此,只需删除它:

class Dog < ActiveRecord::Base
  attr_accessible :name, :category_id

答案 1 :(得分:1)

我需要改变这个:

@dog = current_user.dogs.new(params[:dog])

到这个instad:

@dog = current_user.dogs.build(params[:dog])

然后它开始为我工作。