【问题标题】:Funny error with db:populate and I18n : fr.activerecord.errors.messages.record_invaliddb:populate 和 I18n 的有趣错误:fr.activerecord.errors.messages.record_invalid
【发布时间】:2013-05-17 14:48:18
【问题描述】:

我正在关注 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

有没有人能看懂这个废话并给我解释一下?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • “但仅此而已”是什么意思?这解决了你的问题吗?
    • 不,它没有解决任何问题。我有同样的胡言乱语,但translation missing: fr.activerecord.errors.messages.record_invalid 被法语翻译取代......
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      admin.toggle!(:admin)
      

      试试看,如果有效,请告诉我

      【讨论】:

      • 使用 lib/tasks/sample_data.rake 似乎与批量分配无关。我发现了巧妙的实用程序here
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多