更新:我在 Ruby 1.8 上运行。存储库的链接是 github.com/lauherk/sample_app
我正在阅读 Micheal hartl 的 Ruby on Rails 教程,在第 9 章中,我遇到了用示例用户填充数据库的问题http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users ?version=3.2#sec:sample_users
我的 rake 的代码是:
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
User.create!(:name => "Example User",
:email => "example@railstutorial.org",
: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
然而,在命令行中仍然有相同的结果。
让我的数据库填充示例用户的任何提示?
非常感谢。