I'm getting this error while doing rake:seed. can't mass-assign protected attributes
I know that they are other posts about this error and the solution is to add attr_accessible to the model, but I dont know how to add it in ActiveRecord::Schema.define
Actually I'm learning rails from the book pragmatic book I got this code from that book,
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
I got to add attr_accessible to description, so how do I do that
UPDATE:
my products.rb has attr_accessible, already, but its not working!?
# app/models/product.rb
class Product < ActiveRecord::Base
attr_accessible :descripton, :image_url, :price, :title
end
UPDATE:
*I figured it out, it was a typo problem, thanx anyway, its working but I didntget the actual solution that is adding attr_accessable in scema rather adding it in model. If thats a bad idea, why is it?*
attr_accessible doens't go on schema.rb. It goes on your model. Based on the code you posted above you probably want:
# app/models/product.rb
class Product < ActiveRecord::Base
attr_accessible :title, :description # Add as needed
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.