heroku"无法将nil转换为字符串"

时间:2014-07-29 04:51:59

标签: ruby-on-rails heroku devise null

我在rails4上使用ruby,并尝试部署到heroku。但是,当我试图跑       " heroku运行rake db:migrate",我收到了以下错误。

Running `rake db:migrate` attached to terminal... up, run.8591
rake aborted!
TypeError: can't convert nil into String 
/app/config/initializers/devise.rb:13:in `+'
/app/config/initializers/devise.rb:13:in `block in <top (required)>'
/app/vendor/bundle/ruby/1.9.1/bundler/gems/devise-66db52ce31b5/lib/devise.rb:292:in   `setup'
/app/config/initializers/devise.rb:3:in `<top (required)>'
/app/vendor/bundle/ruby/1.9.1/gems/activesupport- 4.1.4/lib/active_support/dependencies.rb:241:in `load'
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `block in load'
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
 /app/vendor/bundle/ruby/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
 /app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/engine.rb:648:in `block in load_config_initializer'
 /app/vendor/bundle/ruby/1.9.1/gems/activesupport-4.1.4/lib/active_support/notifications.rb:161:in `instrument'
 /app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/engine.rb:647:in `load_config_initializer'
 /app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/engine.rb:611:in `each'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/engine.rb:611:in `block in <class:Engine>'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `instance_exec'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `run'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/initializable.rb:55:in `block in run_initializers'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/initializable.rb:44:in `each'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/initializable.rb:44:in `tsort_each_child'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/initializable.rb:54:in `run_initializers'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/application.rb:300:in `initialize!'
/app/config/environment.rb:5:in `<top (required)>'
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/application.rb:276:in `require_environment!'
/app/vendor/bundle/ruby/1.9.1/gems/railties-4.1.4/lib/rails/application.rb:379:in `block in run_tasks_blocks'
  Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

这是我的Gemfile

source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '4.1.4'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0',          group: :doc
gem 'spring',        group: :development
gem 'bootstrap-sass'
gem 'devise', github: 'plataformatec/devise'
gem 'pundit'


gem 'mail_form'
gem 'simple_form'

group :development do
  gem 'better_errors'
  gem 'binding_of_caller', :platforms=>[:mri_19]
  gem 'quiet_assets'
  gem 'rails_layout'
end
group :development, :test do
   gem 'factory_girl_rails'
   gem 'rspec-rails'
end
group :test do
  gem 'capybara'
  gem 'database_cleaner'
  gem 'faker'
  gem 'launchy'
  gem 'selenium-webdriver'
end
group :production do
  gem 'rails_12factor'
end

gem 'pg'
gem 'validates_formatting_of'
gem 'acts_as_votable', '~> 0.10.0'

gem 'therubyracer',  platforms: :ruby
gem "less-rails"
gem 'twitter-bootstrap-rails'

gem 'mini_magick'
gem 'ransack'
gem 'carrierwave'
gem 'jquery-fileupload-rails'


gem 'bundler'
gem 'activeadmin', github: 'gregbell/active_admin'


gem 'haml'                # or gem 'slim'
gem 'awesome_nested_set'  # or same gem

这是database.yml

 default: &default
    adapter: postgresql
    encoding: unicode
    pool: 5
    timeout: 5000

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production

这是application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'
require 'devise'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Korffice
  class Application < Rails::Application

   config.generators do |g|
      g.test_framework :rspec,
        fixtures: true,
        view_specs: false,
        helper_specs: false,
        routing_specs: false,
        controller_specs: false,
        request_specs: false
     g.fixture_replacement :factory_girl, dir: "spec/factories"
    end

    config.assets.initialize_on_precompile = false
    config.serve_static_assets = true

  end
end

这是secrets.yml

development:
  admin_name: First User
  admin_email: user@example.com
  admin_password: changeme
  email_provider_username: <%= ENV["GMAIL_USERNAME"] %>
  email_provider_password: <%= ENV["GMAIL_PASSWORD"] %>
  domain_name: example.com
  secret_key_base:   98b154040ed274afa3f122e5b3a4f18cac77ac62465b709d94c61e93300bec895ac3b3aa127ad2a8ac5e1a18c894c01c5c61f1f71f0e391dfad6d2b43f781816

test:
  domain_name: example.com 
  secret_key_base: 1a32266f762d172de9f3f9eb52750ee7f8ec8196e77cc940c6eb754ce8ffa4fc93d82682daaad83beba9b2bf25303455a3c149be4092445637de875c6b375489

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  admin_name: <%= ENV["ADMIN_NAME"] %>
  admin_email: <%= ENV["ADMIN_EMAIL"] %>
  admin_password: <%= ENV["ADMIN_PASSWORD"] %>
  email_provider_username: <%= ENV["GMAIL_USERNAME"] %>
  email_provider_password: <%= ENV["GMAIL_PASSWORD"] %> 
  domain_name: <%= ENV["DOMAIN_NAME"] %>
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

这是devise.rb

 Devise.setup do |config|
      config.mailer_sender = 'no-reply@' + Rails.application.secrets.domain_name
      require 'devise/orm/active_record'
     config.case_insensitive_keys = [ :email ]
     config.strip_whitespace_keys = [ :email ]
     config.skip_session_storage = [:http_auth]
     config.stretches = Rails.env.test? ? 1 : 10
     config.reconfirmable = true
     config.password_length = 8..128
     config.reset_password_within = 6.hours
     config.sign_out_via = :delete
  end

请帮助:(

3 个答案:

答案 0 :(得分:4)

当您部署到heroku时,它需要domain_name,对于开发,您已将其设置为字符串:example.com

生产中,它希望域名设置在变量:<%= ENV["DOMAIN_NAME"] %>中,而不是在本地计算机上设置heroku上的域名。

要在heroku中使用env变量,您可以使用figaro gem

答案 1 :(得分:2)

为了给你另一个视角,我看了这个答案:Deploying Rails App to Heroku Keeps Crashing


<强>设计

我不知道Heroku是否要求您设置domain_name(我暂时没有在那里部署),但无论如何,我都说问题与Devise

有关
  

app / config / initializers / devise.rb:13:在`+&#39;

您在为Devise分配email时看起来像是在收到错误,您可以在此处执行此操作:

#config/initializers/devise.rb
config.mailer_sender = 'no-reply@' + Rails.application.secrets.domain_name

您遇到的问题是您已在机密中为您的开发环境设置了domain_name;并在生产中使用ENV变量作为domain_name

-

ENV Vars

要解决此问题,您需要确保在Heroku上设置了ENV个变量。为此,您需要感谢what an ENV variable is(它与Figaro无关)

  

ENV(环境)变量位于操作系统和允许   用于访问(通常是秘密的)数据池的应用程序,可以是   在整个申请中使用。 ENV变量通常用于   存储敏感数据(例如API键),主要是为了解耦   来自应用程序本身的数据(以使系统更安全)

看起来您还没有为DOMAIN_NAME设置获取环境变量。要做到这一点,您最好使用Heroku的CLI(命令行界面)set it

$ heroku config:set DOMAIN_NAME=your_domain.com

答案 2 :(得分:0)

我遇到了同样的问题。我在secret.yml中设置了DOMAIN_NAME等,这个文件被git忽略了,因为我想保持凭证私有。所以Heroku无法找到这些配置值。

使用figaro gem似乎是解决这个问题的好方法。但我不想添加另一个gem并创建一个新的yml文件。

我所做的是直接设置Heroku环境变量并且它有效:

$ heroku config:add GMAIL_USERNAME='myname@gmail.com' GMAIL_PASSWORD='mypassword'
$ heroku config:add DOMAIN_NAME='example.com'