[英]how to add attr_accessible to database table in schema.rb
我在進行rake:seed時遇到此錯誤。 無法批量分配受保護的屬性
我知道他們是關於此錯誤的其他文章,解決方案是將attr_accessible添加到模型中,但是我不知道如何在ActiveRecord::Schema.define
添加它
實際上,我是從實用的書中學習Rails的,我從那本書中獲得了這段代碼,
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
更新:
*我發現,這是一個錯字問題,無論如何,thanx都可以正常工作,但是我沒有得到實際的解決方案,即在scema中添加attr_accessable而不是在模型中添加它。 如果那是個壞主意,為什么呢?*
attr_accessible不要在schema.rb上進行。 它取決於您的模型。 根據您上面發布的代碼,您可能想要:
# app/models/product.rb
class Product < ActiveRecord::Base
attr_accessible :title, :description # Add as needed
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.