我试图将 twitter 身份验证集成到我的 Ruby on Rails 应用程序中(来自 Ruby on Rails 教程)。
结果,我删除了一个表(用户表)并不得不重新创建它。我现在想填充数据库,我有这个代码来填充表
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 migrate
$bundle exec rake:db reset
但是 $bundle exec rake:db populate 不起作用。
我遇到了以下错误:
$ bundle exec rake db:populate --trace
** Invoke db:populate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:populate
rake aborted!
wrong number of arguments (0 for 1)
c:/Ruby193/lib/ruby/gems/1.9.1/gems/devise-2.0.4/lib/devise/models/database_auth
enticatable.rb:105:in `password_digest'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/errors.rb
:254:in `block in add_on_blank'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/errors.rb
:253:in `each'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/errors.rb
:253:in `add_on_blank'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validatio
ns/presence.rb:9:in `validate'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:310:in `_callback_before_39'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:484:in `_run__584816360__validate__204154762__callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:405:in `__run_callback'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:385:in `_run_validate_callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:81:in `run_callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validatio
ns.rb:212:in `run_validations!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validatio
ns/callbacks.rb:53:in `block in run_validations!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:425:in `_run__584816360__validation__204154762__callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:405:in `__run_callback'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:385:in `_run_validation_callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:81:in `run_callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validatio
ns/callbacks.rb:53:in `run_validations!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validatio
ns.rb:179:in `valid?'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/validat
ions.rb:69:in `valid?'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/validat
ions.rb:77:in `perform_validations'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/validat
ions.rb:56:in `save!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/attribu
te_methods/dirty.rb:33:in `save!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/transac
tions.rb:246:in `block in save!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/transac
tions.rb:295:in `block in with_transaction_returning_status'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/connect
ion_adapters/abstract/database_statements.rb:190:in `transaction'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/transac
tions.rb:208:in `transaction'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/transac
tions.rb:293:in `with_transaction_returning_status'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/transac
tions.rb:246:in `save!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/validat
ions.rb:41:in `create!'
c:/tuffy/shopgodt/lib/tasks/sample_data.rake:4:in `block (2 levels) in <top (req
uired)>'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:205:in `call'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:205:in `block in execute'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:200:in `each'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:200:in `execute'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:158:in `block in invoke_with_call_chain'
c:/Ruby193/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:144:in `invoke'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:116:in `invoke_task'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:94:in `block (2 levels) in top_lev
el'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:94:in `each'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:94:in `block in top_level'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handlin
g'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:88:in `top_level'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:66:in `block in run'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handlin
g'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:63:in `run'
c:/Ruby193/bin/rake:32:in `<main>'
Tasks: TOP => db:populate
对此问题的任何帮助表示赞赏。再次感谢!
==================================================== ==========
2012 年 5 月 2 日更新:因为我想添加第三方登录(facebook、twitter),所以我添加了 Devise gem,但我不确定我运行的哪个命令导致这两行被添加到我的应用程序/模型中/User.rb 文件
:token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
一旦我将它们注释掉,一切都运行良好。我想了解这两行在做什么?