【问题标题】:rake seed aborted! Couldn't convert nil to string耙子流产了!无法将 nil 转换为字符串
【发布时间】:2013-06-05 14:38:41
【问题描述】:

我有简单的 rails 2 composer 应用程序。我可以很好地在本地对应用程序进行 rake 迁移和播种,并且设置了种子文件中的管理员用户。但是,数据库不会在 Heroku 上播种。我收到以下错误(运行时带有跟踪 - heroku run rake db:setup --trace ):

** Execute db:abort_if_pending_migrations
ROLES
rake aborted!
can't convert nil into String

这是我的代码:

种子.rb

# This file should contain all the record creation needed to seed the database with its     default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
#   cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
#   Mayor.create(name: 'Emanuel', city: cities.first)
# Environment variables (ENV['...']) are set in the file config/application.yml.
# See http://railsapps.github.io/rails-environment-variables.html
puts 'ROLES'
YAML.load(ENV['ROLES']).each do |role|
  Role.find_or_create_by_name({ :name => role }, :without_protection => true)
  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
user.save!

application.yml

GMAIL_USERNAME: Your_Username
GMAIL_PASSWORD: Your_Password
ADMIN_NAME: First User
ADMIN_EMAIL: user@example.com
ADMIN_PASSWORD: changeme
ROLES: [admin, user]

我对 Rails 相当陌生。该应用程序最初确实是种子,但我已经进行了一些迁移并回滚了一次。

非常感谢任何帮助。

【问题讨论】:

  • 您确定设置了 ENV['ROLES'] 吗?而不是 puts 'ROLES' 尝试 puts ENV['ROLES']。
  • 不应该将 ROLES 设置为 ['admin', 'user'] 而不是 [admin, user] 吗?
  • 感谢 cmets。设法通过此评论修复使用 figaro gem - github.com/RailsApps/rails-stripe-membership-saas/issues/…
  • @Richard Margan 请将此作为答案并接受它。

标签: ruby-on-rails heroku rake seed abort


【解决方案1】:

如果您使用“db:reset --trace”跟踪角色并像这样添加“放置角色”:

YAML.load(ENV['ROLES']).each 做 |role| 放置角色

可以看到第一个角色是admin,但是在rake db:reset 之后放上消息“uninitialized constant Role”

所以问题不在于 ENV['ROLES'] !

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多