1

我正在关注 M. Hartl 教程,但出现了一个我无法理解的错误。我想用假用户填充我的数据库,先使用rake db:reset,然后使用rake db:populate,但它失败了。

这是代码:

lib/tasks/sample_data.rake

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    admin = User.create!(name: "Example User",
                 email: "example@apptesting.com",
                 password: "motdepasse",
                 password_confirmation: "motdepasse",
                 admin: true)
    99.times do |n|
      name  = "User #{n+1}"
      email = "user-#{n+1}@apptesting.com"
      password  = "motdepasse"
      User.create!(name: name,
                   email: email,
                   password: password,
                   password_confirmation: password)
    end
  end
end

之后的结果rake db:populate

$ rake db:populate --trace
** Invoke db:populate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:populate
rake aborted!
translation missing: fr.activerecord.errors.messages.record_invalid
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/validations.rb:57:in `save!'
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/transactions.rb:275:in `block in save!'
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/transactions.rb:209:in `transaction'
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/transactions.rb:275:in `save!'
/home/flo/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0.beta1/lib/active_record/validations.rb:41:in `create!'
/home/flo/RoR/letroquet/lib/tasks/sample_data.rake:4:in `block (2 levels) in <top (required)>'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/home/flo/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/gems/rake-10.0.4/bin/rake:33:in `<top (required)>'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/bin/rake:23:in `load'
/home/flo/.rvm/gems/ruby-2.0.0-p0@global/bin/rake:23:in `<main>'
/home/flo/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `eval'
/home/flo/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:populate

有没有人可以理解这种胡言乱语并向我解释?

4

3 回答 3

2

试着把这个文件:https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/fr.yml放在你的config/locales/文件夹中,它只是说你的记录是无效的,但它在 fr 中找不到合适的消息。语言环境

于 2013-05-16T08:25:17.493 回答
0

看起来您的 admin 属性是错误的。除非您将管理员添加到attr_accessible. 如果您确实添加了它,那将是一个糟糕的安全漏洞。基本上任何人都可以通过命令行使自己成为管理员。以下是添加 admin 为 true 的方法:

admin.toggle!(:admin)

试试看,让我知道它是否有效

于 2013-05-17T06:48:18.700 回答
0

看来您有验证错误。检查您的用户模型以进行验证,并尝试找到一个失败的模型。

如果您找不到哪一项验证失败,请尝试从控制台创建具有相同属性的用户。

user = User.new({ 'Your attributes' })
user.valid?
user.errors.messages
于 2013-05-17T06:34:07.720 回答