我想用Faker为数据库播种,问题是当我这样做时出错:
rake db:reset
我收到此消息:
rake aborted!
I18n::MissingTranslationData: translation missing: en.faker.name.name
/Library/Ruby/Gems/2.0.0/gems/i18n-0.7.0/lib/i18n.rb:311:in `handle_exception'
/Library/Ruby/Gems/2.0.0/gems/i18n-0.7.0/lib/i18n.rb:161:in `translate'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:128:in `rescue in translate'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:120:in `translate'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:86:in `fetch'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:99:in `parse'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker/name.rb:8:in `name'
/Users/hbendev/code/wikitec/db/seeds.rb:6:in `block in <top (required)>'
/Users/hbendev/code/wikitec/db/seeds.rb:4:in `times'
/Users/hbendev/code/wikitec/db/seeds.rb:4:in `<top (required)>'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/Library/Ruby/Gems/2.0.0/gems/railties-4.2.0/lib/rails/engine.rb:547:in `load_seed'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:139:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:setup => db:seed
我不知道为什么会出现这个错误,因为之前我没有遇到Faker的任何问题,我只是想重置数据库来更新种子。
我谷歌但我找不到任何可以解决问题的相关内容。
我试图添加:
I18n.reload!
在我的seeds.rb文件中require 'faker'
之后,但没有运气。
看起来问题出在Faker本身,因为数据库正在正确创建,当我执行rake db:drop db:create db:migrate
它有效时,直到那里,但当我尝试使用Faker为{{1}种子数据库播种时}或rake db:seed
,我收到错误。
我该怎么办?提前谢谢。
更新 - 我包含了seeds.rb和en.yml文件
seeds.rb:
rake db:reset
en.yml:
require 'faker'
# Create Users
5.times do
user = User.new(
name: Faker::Name.name,
email: Faker::Internet.email,
password: Faker::Lorem.characters(10)
)
user.skip_confirmation!
user.save!
end
users = User.all
# Create Wikis
25.times do
Wiki.create!(
title: Faker::Lorem.sentence,
body: Faker::Lorem.paragraph,
:private => false,
user: users.sample
)
end
# Create Admin account
admin = User.new(
name: 'Admin User',
email: 'admin@example.com',
password: 'helloworld',
role: 'admin'
)
admin.skip_confirmation!
admin.save!
# Create Premium account
premium = User.new(
name: 'Premium User',
email: 'premium@example.com',
password: 'helloworld',
role: 'premium'
)
premium.skip_confirmation!
premium.save!
# Create Standard account
standard = User.new(
name: 'Standard User',
email: 'standard@example.com',
password: 'helloworld',
role: 'standard'
)
standard.skip_confirmation!
standard.save!
puts "Seed finished"
puts "#{Wiki.count} wikis created"
puts "#{User.count} users created"
答案 0 :(得分:1)
在此处查看I18n Faker配置信息:
https://github.com/stympy/faker#customization
看起来你应该强制执行I18n Faker语言环境,以防你在应用程序中使用非标准语言环境。
只需将Faker :: Config.locale设置为您想要的语言环境,Faker即可 照顾好其余的事。
答案 1 :(得分:1)
这对我有用......
在Gemfile
添加:require => false
group :development, :test do
#gem 'faker', '~> 1.4.3'
gem 'faker', :require => false
end
手动添加require "faker"
...
答案 2 :(得分:0)
就我而言,I18n available_locales配置不包含en
:
config.i18n.available_locales = %i[de de_en]
我将其还原为
config.i18n.available_locales = %i[de en]
成功了。
答案 3 :(得分:-1)
我遇到了同样的问题。 有必要将faker从
移到Gemfilegroup: development: test do
gem 'faker'
end
对我来说它解决了一个问题 祝你好运