2

我试图将 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

一旦我将它们注释掉,一切都运行良好。我想了解这两行在做什么?

4

3 回答 3

3

有两个提示说明出了什么问题

c:/tuffy/shopgodt/lib/tasks/sample_data.rake:4:in 'block (2 levels) in <top (required)>'

告诉你它在第 4 行,这是调用create!

再加上

c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validations/presence.rb:9:in 'validate'

表示验证失败。是否存在期望存在您未提供的某些字段的验证?检查您的User模型validates :some_field_name, :presence => true

于 2012-05-02T15:55:15.157 回答
2

看起来它可能是User类本身的东西。可能在验证中。

您是否有任何自定义验证或任何其他自定义回调?值得检查这些。

您可以User.create!在控制台中独立于 rake 任务来确认这是否是问题所在。

于 2012-05-02T15:47:50.353 回答
2

好的 - 我遇到了同样的问题。我刚刚完成了相同的教程,并且在过去的 3 个小时里把我的头发拉了出来。浏览 Gem 的代码,一切都让我意识到发生了什么。

这是怎么回事:

在第 6 章中,本教程要求您添加'has_secure_password'到您的用户模型中。将此助手从您与 Devise 一起使用的任何模型中删除。虽然 Devise 没有在 Gem 中明确调用该助手,但它的代码几乎模仿了“has_secure_password”的作用。

在你这样做之后,杀死你的服务器、rails 控制台等。重新开始,生活应该会再次快乐起来。

于 2012-05-14T23:18:06.670 回答