当运行rake db:seed
以放入测试用户时,我收到以下错误:
ROLES
rake aborted!
no implicit conversion of nil into String
/Users/joshburns/Code/file-server/db/seeds.rb:10:in `<top (required)>'
/Users/joshburns/.rvm/gems/ruby-2.1.0@file-server/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/Users/joshburns/.rvm/gems/ruby-2.1.0@file-server/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `block in load'
/Users/joshburns/.rvm/gems/ruby-2.1.0@file-server/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/joshburns/.rvm/gems/ruby-2.1.0@file-server/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/Users/joshburns/.rvm/gems/ruby-2.1.0@file-server/gems/railties-4.0.3/lib/rails/engine.rb:540:in `load_seed'
/Users/joshburns/.rvm/gems/ruby-2.1.0@file-server/gems/activerecord-4.0.3/lib/active_record/tasks/database_tasks.rb:154:in `load_seed'
/Users/joshburns/.rvm/gems/ruby-2.1.0@file-server/gems/activerecord-4.0.3/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
我的种子文件:
puts 'ROLES'
YAML.load(ENV['ROLES']).each do |role|
Role.find_or_create_by_name(role)
puts 'role: ' << role
end
puts 'DEFAULT USERS'
user = User.find_or_create_by_email :name => ENV['ADMIN_NAME'].dup, :email => ENV['ADMIN_EMAIL'].dup, :password => ENV['ADMIN_PASSWORD'].dup, :password_confirmation => ENV['ADMIN_PASSWORD'].dup
puts 'user: ' << user.name
user.add_role :admin
答案 0 :(得分:0)
如果这是你的seeds.db文件,为什么要输出字符串?
puts 'ROLES'
puts 'DEFAULT USERS'
puts 'user: ' << user.name
seeds.rb
与rake任务,模型和&amp;控制器 - 它们不会直接向用户呈现任何内容
虽然我们需要查看更多代码,但我会这样做:
YAML.load(ENV['ROLES']).each do |role|
Role.find_or_create_by_name(role)
puts 'role: ' << role
end
user = User.find_or_create_by_email :name => ENV['ADMIN_NAME'], :email => ENV['ADMIN_EMAIL'], :password => ENV['ADMIN_PASSWORD'], :password_confirmation => ENV['ADMIN_PASSWORD']
user.add_role :admin #-> need to see your method
如果您希望使seed.rb文件可读,则应使用注释将其拆分