db的有趣错误:populate和I18n:fr.activerecord.errors.messages.record_invalid

时间:2013-05-16 07:52:15

标签: ruby-on-rails populate rails-i18n

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

以下是代码:

LIB /任务/ 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

是否有人能够理解这种胡言乱语并向我解释?

3 个答案:

答案 0 :(得分:2)

尝试将此文件:https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/fr.yml放在 config / locales / 文件夹中,它只是说您的记录无效,但它无法在fr中找到相应的消息。区域设置

答案 1 :(得分:0)

好像你有验证错误。检查您的用户模型以获取验证,然后尝试查找失败的模型。

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

user = User.new({ 'Your attributes' })
user.valid?
user.errors.messages

答案 2 :(得分:0)

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

admin.toggle!(:admin)

尝试一下,让我知道它是否有效