更新:我在Ruby 1.8上運行。到存儲庫的聯繫是github.com/lauherk/sample_appRuby on Rails教程(by Hartl)當做name = Faker :: Name.name encoutner時無法批量分配受保護的屬性錯誤
我經歷的Ruby on Rails的教程由邁克爾哈特爾,並在第9章,我遇到的問題與填充帶樣本用戶數據庫http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users?version=3.2#sec:sample_users
爲我耙的代碼是:
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
User.create!(:name => "Example User",
:email => "[email protected]",
:password => "foobar",
:password_confirmation => "foobar")
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
end
同時運行後:
bundle exec rake db:reset
bundle exec rake db:populate
我從以下命令行錯誤:
rake aborted!
Can't mass-assign protected attributes: Lawrence Kertzmann
/Library/Ruby/Gems/1.8/gems/activemodel-3.2.2/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
(這個特定的名稱更改每次我運行rake時間)
我已經確認並已確保在我的用戶模型我的代碼
attr_accessible :name, :email, :password, :password_confirmation
,並甚至試圖設置
config.active_record.whitelist_attributes = false
但是,在命令行仍然有相同的結果。
任何提示讓我的數據庫填充示例用戶?
非常感謝。
我把你的代碼放在我的耙子任務中,它工作。 「示例用戶」創建時沒有和錯誤。它似乎已經根據錯誤中的名稱進入了循環。另一方面,錯誤說它試圖設置的屬性是「Lawrence Kertzmann」,而不是「名稱」或其他屬性... – EricM 2012-04-17 15:05:48
您是否使用Ruby 1.9?您正在使用新的Ruby 1.9哈希語法。 – retro 2012-04-17 23:03:47
是的,它仍然不能正常工作給出錯誤'不能大規模分配受保護的屬性:勞倫斯凱茲曼' – user1337902 2012-04-18 05:03:26