0

rake:seedの実行中にこのエラーが発生します。保護された属性を一括割り当てできません

私は彼らがこのエラーに関する他の投稿であることを知っています、そして解決策はモデルにattr_accessibleを追加することです、しかし私はそれを追加する方法を知りませんActiveRecord::Schema.define

実際、私は本の実用的な本からレールを学んでいます。その本からこのコードを取得しました。

ActiveRecord::Schema.define(:version => 20120814110309) do

  create_table "products", :force => true do |t|
    t.string   "title"
    t.text     "descripton"
    t.string   "image_url"
    t.decimal  "price",      :precision => 8, :scale => 2
    t.datetime "created_at",                               :null => false
    t.datetime "updated_at",                               :null => false
  end

end

説明にattr_accessibleを追加する必要があるので、どうすればよいですか

アップデート:

私のproducts.rbにはすでにattr_accessibleがありますが、機能していません!?

# app/models/product.rb
class Product < ActiveRecord::Base  
    attr_accessible :descripton, :image_url, :price, :title
end

アップデート:

*私はそれを理解しました、それはタイプミスの問題でした、とにかくそれは機能しました、しかし私はそれをモデルに追加するのではなく、scemaにattr_accessableを追加する実際の解決策を得ませんでした。それが悪い考えだとしたら、それはなぜですか?*

4

1 に答える 1

1

attr_accessibleはschema.rbに移動しません。それはあなたのモデルに行きます。上に投稿したコードに基づいて、おそらく次のようにします。

# app/models/product.rb
class Product < ActiveRecord::Base
 attr_accessible :title, :description # Add as needed
end
于 2012-08-16T20:47:14.500 に答える