ruby-on-rails - 名称错误 : uninitialized constant Faker

标签 ruby-on-rails ruby ruby-on-rails-4 rspec ruby-on-rails-5

我正在尝试在 Rails 4 中为我的数据库运行一个简单的 bundle exec rake db:seed。但是,在运行它时,我得到以下输出:

********-C02MGBVJFD57:myapp ***********$ bundle exec rake db:seed
Your Gemfile lists the gem factory_girl_rails (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
rake aborted!
NameError: uninitialized constant Faker
/Users/**********/workspace/myapp/db/seeds.rb:16:in `block in <top (required)>'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `times'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `<top (required)>'
/Users/**********/.rvm/gems/ruby-2.1.2@myapp/gems/railties-4.1.4/lib/rails/engine.rb:543:in `load_seed'
/Users/**********/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:184:in `load_seed'
/Users/**********/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

这是我的 seeds.rb 文件:

User.create!(
  name:                  "Example User",
  email:                 "example@railstutorial.org",
  password:              "foobar",
  password_confirmation: "foobar",
  admin:                 true
)

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

第 16 行是:

name = Faker::Name.name

知道我为什么会收到此错误吗?谢谢你。

最佳答案

刚刚遇到类似的问题 - 我正在运行

rails g model model_name

并得到错误:

uninitialized constant Faker (NameError)

问题是因为我将 gem 添加到 test 组。

将其放入developmenttest组解决了问题:

group :development, :test do
  # ...
  gem 'faker'
  # ...
end

关于ruby-on-rails - 名称错误 : uninitialized constant Faker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27452632/

相关文章:

ruby-on-rails - 告诉 AuthLogic 不要使用密码确认

ruby - ffmpeg:处理输入时发现无效数据

ruby-on-rails - Ruby on Rails 应用程序与核心开发

在 Ruby On Rails 中使用幻灯片放映的 jQuery

ruby-on-rails - 使用 Sanitize 将转换器中的节点列入白名单

ruby - 内置在 ruby​​ 目录中列出目录的方法

ruby - 将 unicode 字符串转换为 Ruby 中的字符?

ruby-on-rails - 在 rails 4 中验证 attr_accessor 的标签

ruby-on-rails - Rails 4 使用多个 URL 参数测试嵌套资源

css - 在此 Rails 应用程序 View 循环中,如何包含 CSS 按钮样式?