无法在 Rails 教程中为数据库做种 [英] Unable to seed database in rails tutorial
本文介绍了无法在 Rails 教程中为数据库做种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
11.2.2
首先我迁移了 db(bundle exec rake db:migrate:reset),然后我尝试播种它(bundle exec rake db:seed),然后我收到了这条消息:
First I migrated db(bundle exec rake db:migrate:reset), then I try to seed it(bundle exec rake db:seed) and I get this message:
rake aborted!
SyntaxError: /home/aki/sample_app/db/seeds.rb:25: syntax error, unexpected end-of-input, expecting keyword_end
/home/aki/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/home/aki/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/home/aki/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/aki/.rvm/gems/ruby-2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/home/aki/.rvm/gems/ruby-2.1.3/gems/railties-4.2.0/lib/rails/engine.rb:547:in `load_seed'
/home/aki/.rvm/gems/ruby-2.1.3/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/home/aki/.rvm/gems/ruby-2.1.3/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
这是我的db/seeds.rb
文件:
User.create!(name: "Example User",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true,
activated: true,
activated_at: Time.zone.now)
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,
activated: true,
activated_at: Time.zone.now)
users = User.order(:created_at).take(6)
50.times do
content = Faker::Lorem.sentence(5)
users.each { |user| user.microposts.create!(content: content) }
end
推荐答案
试试这个:
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,
activated: true,
activated_at: Time.zone.now)
end #This is the missing end and your sytax error
这篇关于无法在 Rails 教程中为数据库做种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文