[英]NameError: uninitialized constant Faker
我正在嘗試在 Rails 4 中為我的數據庫運行一個簡單的 bundle exec rake db:seed。但是,在運行它時,我得到以下 output:
********-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
組。
將其放入development
和test
組解決了問題:
group :development, :test do
# ...
gem 'faker'
# ...
end
我在編寫 rspec 並在規范文件中添加require 'faker'
時遇到了同樣的問題。
在 Gemfile 中添加gem 'faker'
並運行bundle install
根據官方的 faker 文檔,它說:
注意:如果您遇到未初始化的常量 Faker::[some_class] 錯誤,則您的 gem 版本落后於此處記錄的版本。 為確保您的 gem 是此處記錄的那個,請將 Gemfile 中的行更改為:
gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main'
但是問題仍然存在於我的應用程序中,因為我已經這樣做了並且我再次收到錯誤,但只有在嘗試運行遷移到 heroku 時:
heroku run rails db:migrate db:seed
當我在本地運行命令時,我沒有遇到問題,並且執行了遷移和種子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.