0

犬を作成してカテゴリを追加しようとしていますが、次のエラーが表示されます。

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 %>

試してみると、まだ Mass-assignment エラーが発生します。

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)

技術的に機能している場合、これは問題ですか?

4

2 に答える 2

1

ここに余分なものがありsます:

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

category_iddogsテーブルの列なので、削除してください:

class Dog < ActiveRecord::Base
  attr_accessible :name, :category_id
于 2012-07-29T12:35:31.063 に答える
1

これを変更する必要がありました:

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

この代わりに:

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

そして、それは私のために働き始めました。

于 2012-08-08T19:02:49.110 に答える