0

実現したいこと

git push heroku master した時のNoMethodError: undefined method `to_sym' for nil:NilClassを解消して、デプロイに成功させたい

背景

Action_mailerとHeroku shcedulerを使って定期的にメールを1通ユーザーに送る実装をしようと考えていました。そこで、gem figaroを使って実装進めていました。

環境

Rails:5.2.4
DB:PostgreSQL
Rubyのbuildpackはインストール済
(heroku create --buildpack https://github.com/heroku/heroku-buildpack-ruby.git)
heroku addons:add scheduler:standard

今起こっている問題

git push heroku masterする際に、3つのエラーが発生しており、そのうち2つは自分の昨日の記事と同じものなのですが、もう1つ、下記のようなエラーに初めて出くわしました。

remote:        rake aborted!
remote:        NoMethodError: undefined method `to_sym' for nil:NilClass

出てきたエラーに対しログを辿っても、意味が理解できず、解決に苦しんでおります。何か、アドバイス頂ければ幸いです。

to_sym:シンボルを返すrubyのメソッドで、production.rb内の

config.action_mailer.delivery_method = ENV['delivery_method'].to_sym

これが悪さしている?気がします。

試したこと

gem figaroをインストール

bundle exec figaro install

③生成されたapplication.ymlで、以下のように編集

application.yml

# Add configuration values here, as shown below.
#
# pusher_app_id: "2954"
# pusher_key: 7381a978f7dd7f9a1117
# pusher_secret: abdc3b896a0ffb85d373
# stripe_api_key: sk_test_2J0l093xOyW72XUYJHE4Dv2r
# stripe_publishable_key: pk_test_ro9jV5SNwGb1yYlQfzG17LHK
# メール送信サーバの設定(gmail)
delivery_method:           smtp
smtp_enable_starttls_auto: true
smtp_address:              smtp.gmail.com
smtp_port:                 587
smtp_domain:               gmail.com
smtp_authentication:       plain
smtp_user_name:            (送信元アドレス)@gmail.com
smtp_password:             (2段階認証で設定したアプリパスワード)

# production:
#   stripe_api_key: sk_live_EeHnL644i6zo4Iyq4v1KdV9H
#   stripe_publishable_key: pk_live_9lcthxpSIHbGwmdO941O1XVU

production.rbには、下記の通り編集

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = ENV['delivery_method'].to_sym
  config.action_mailer.default_url_options = { host: 'quiet-escarpment-59252.herokuapp.com'}
  config.action_mailer.smtp_settings = {
    enable_starttls_auto: ENV['smtp_enable_starttls_auto'],
    address: ENV['smtp_address'],
    port: ENV['smtp_port'],
    domain: ENV['smtp_domain'],
    authentication: ENV['smtp_authentication'],
    user_name: ENV['smtp_user_name'],
    password: ENV['smtp_password']
  }

その後、全ての変更は、 git add -A, git commit -m "Message"で行い,git push heroku masterでデプロイに挑戦しましたが、すべてデプロイできませんでした、、、お力貸して頂けますと幸いです。

エラー全文

ログ

-----> Ruby app detected
-----> Installing bundler 2.0.2
-----> Removing BUNDLED WITH version in the Gemfile.lock
-----> Compiling Ruby/Rails
-----> Using Ruby version: ruby-2.6.5
-----> Installing dependencies using bundler 2.0.2
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
       The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
       Fetching gem metadata from https://rubygems.org/.........
       Using rake 13.0.1
       Using concurrent-ruby 1.1.6
       Using minitest 5.14.1
       Using thread_safe 0.3.6
       Using builder 3.2.4
       Using mini_portile2 2.4.0
       Using erubi 1.9.0
       Using crass 1.0.6
       Fetching rack 2.2.3
       Using nio4r 2.5.2
       Using websocket-extensions 0.1.5
       Using mini_mime 1.0.2
       Using arel 9.0.0
       Using mimemagic 0.3.5
       Using execjs 2.7.0
       Using bcrypt 3.1.13
       Using msgpack 1.3.3
       Using popper_js 1.16.0
       Using method_source 1.0.0
       Using thor 1.0.1
       Using ffi 1.13.0
       Using tilt 2.0.10
       Using bundler 2.0.2
       Using cocoon 1.2.14
       Using coderay 1.1.3
       Using orm_adapter 0.5.0
       Using devise-bootstrap-views 1.1.0
       Using devise-i18n-views 0.3.7
       Using multi_json 1.14.1
       Using hpricot 0.8.6
       Using kaminari-core 1.2.1
       Using pg 1.2.3
       Using temple 0.8.2
       Using turbolinks-source 5.2.0
       Installing rack 2.2.3
       Using i18n 1.8.2
       Using tzinfo 1.2.7
       Using nokogiri 1.10.9
       Using websocket-driver 0.7.2
       Using puma 4.3.5
       Using mail 2.7.1
       Using marcel 0.3.3
       Using bootsnap 1.4.6
       Using sassc 2.3.0
       Fetching figaro 1.2.0
       Using pry 0.13.1
       Using autoprefixer-rails 9.7.6
       Using uglifier 4.2.0
       Using html2slim 0.2.0
       Using slim 4.1.0
       Installing figaro 1.2.0
       Using turbolinks 5.2.1
       Using activesupport 5.2.4.3
       Using loofah 2.5.0
       Using pry-rails 0.3.9
       Using rails-dom-testing 2.0.3
       Using globalid 0.4.2
       Using activemodel 5.2.4.3
       Using jbuilder 2.10.0
       Using rails-html-sanitizer 1.3.0
       Using activejob 5.2.4.3
       Using activerecord 5.2.4.3
       Using actionview 5.2.4.3
       Using kaminari-activerecord 1.2.1
       Using polyamorous 2.3.2
       Using kaminari-actionview 1.2.1
       Using ransack 2.3.2
       Using kaminari 1.2.1
       Using rack-test 1.1.0
       Using warden 1.2.8
       Using request_store 1.5.0
       Using rack-proxy 0.6.5
       Using actionpack 5.2.4.3
       Using sprockets 4.0.0
       Using actioncable 5.2.4.3
       Using actionmailer 5.2.4.3
       Using activestorage 5.2.4.3
       Using railties 5.2.4.3
       Using gon 6.3.2
       Using chart-js-rails 0.1.7
       Using sprockets-rails 3.2.1
       Using responders 3.0.1
       Using jquery-rails 4.4.0
       Using momentjs-rails 2.20.1
       Using rails-i18n 5.1.3
       Using slim-rails 3.2.0
       Using webpacker 4.2.2
       Using sassc-rails 2.1.2
       Using rails 5.2.4.3
       Using bootstrap 4.5.0
       Using devise 4.7.1
       Using sass-rails 6.0.0
       Using devise-i18n 1.9.1
       Using kaminari-bootstrap 3.0.1
       Bundle complete! 39 Gemfile dependencies, 92 gems now installed.
       Gems in the groups development and test were not installed.
       Bundled gems are installed into `./vendor/bundle`
       Removing rack (2.2.2)
       Bundle completed (3.42s)
       Cleaning up the bundler cache.
-----> Installing node-v10.15.3-linux-x64
-----> Installing yarn-v1.16.0
-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       done.
       rake aborted!
       NoMethodError: undefined method `to_sym' for nil:NilClass
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/config/environments/production.rb:4:in `block in <main>'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/railtie.rb:216:in `instance_eval'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/railtie.rb:216:in `configure'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/config/environments/production.rb:1:in `<main>'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `block in require'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:257:in `load_dependency'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `require'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/engine.rb:608:in `block (2 levels) in <class:Engine>'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/engine.rb:607:in `each'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/engine.rb:607:in `block in <class:Engine>'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/initializable.rb:32:in `instance_exec'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/initializable.rb:32:in `run'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/initializable.rb:61:in `block in run_initializers'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/initializable.rb:50:in `each'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/initializable.rb:50:in `tsort_each_child'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/initializable.rb:60:in `run_initializers'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/application.rb:361:in `initialize!'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/config/environment.rb:5:in `<main>'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `block in require'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:257:in `load_dependency'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `require'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/application.rb:337:in `require_environment!'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/application.rb:520:in `block in run_tasks_blocks'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:62:in `block (2 levels) in define'
       /tmp/build_f385a0a2521e560c2ede3dcc72c22ed8/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
       Tasks: TOP => environment
       (See full trace by running task with --trace)
 !
 !     Precompiling assets failed.
 !
 !     Push rejected, failed to compile Ruby app.
 !     Push failed
remote: Verifying deploy...
remote: 
remote: !       Push rejected to quiet-escarpment-59252.
remote: 
To https://git.heroku.com/quiet-escarpment-59252.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/quiet-escarpment-59252.git'

application.yml全文

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = ENV['delivery_method'].to_sym
  config.action_mailer.default_url_options = { host: 'quiet-escarpment-59252.herokuapp.com'}
  config.action_mailer.smtp_settings = {
    enable_starttls_auto: ENV['smtp_enable_starttls_auto'],
    address: ENV['smtp_address'],
    port: ENV['smtp_port'],
    domain: ENV['smtp_domain'],
    authentication: ENV['smtp_authentication'],
    user_name: ENV['smtp_user_name'],
    password: ENV['smtp_password']
  }

  # user_name: ENV['smtp_user_name'],
  # password: ENV['smtp_password']
  # user_name: ENV['SENDGRID_USERNAME'],
  # password: ENV['SENDGRID_PASSWORD']
  # Code is not reloaded between requests.
  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both threaded web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
  # or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
  # config.require_master_key = true

  # Disable serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = true

  # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb

  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  # config.action_controller.asset_host = 'http://assets.example.com'

  # Specifies the header that your server uses for sending files.
  # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

  # Store uploaded files on the local file system (see config/storage.yml for options)
  config.active_storage.service = :local

  # Mount Action Cable outside main process or domain
  # config.action_cable.mount_path = nil
  # config.action_cable.url = 'wss://example.com/cable'
  # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # Use the lowest log level to ensure availability of diagnostic information
  # when problems arise.
  config.log_level = :debug

  # Prepend all log lines with the following tags.
  config.log_tags = [ :request_id ]

  # Use a different cache store in production.
  # config.cache_store = :mem_cache_store

  # Use a real queuing backend for Active Job (and separate queues per environment)
  # config.active_job.queue_adapter     = :resque
  # config.active_job.queue_name_prefix = "tt_manager_#{Rails.env}"

  config.action_mailer.perform_caching = false

  # Ignore bad email addresses and do not raise email delivery errors.
  # Set this to true and configure the email server for immediate delivery to raise delivery errors.
  # config.action_mailer.raise_delivery_errors = false

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new

  # Use a different logger for distributed setups.
  # require 'syslog/logger'
  # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false
end

1 件の回答 1

2

確かに、

config.action_mailer.delivery_method = ENV['delivery_method'].to_sym

の行でエラーになっていそうです。環境変数 delivery_method を設定しておらず ENV['delivery_method']nil になったため、nil.to_sym をしようとしてエラーになっているように見えます。

本来ならば application.yml に書いてある設定を figaro gem が読み込んで環境変数に出してくれるはずなので、この一連の流れのどこかが上手くいってなさそうですね。

で、figaro gem の初期設定では application.yml は .gitignore されるので Heroku の環境側に application.yml が存在していないのではないでしょうか。このファイルには秘匿値も入りうる想定なので、単に git push するのでは公開されないようになっています。この仮説のもと質問者さんの操作手順を見ると、Heroku の環境に application.yml を送り込むコマンドである、

figaro heroku:set -e production

を実行していなさそうです。README の https://github.com/laserlemon/figaro#deployment あたりに書かれているので、こちらを試してみてください。

5
  • nekketsuuuさん ご連絡ありがとうございます! お話頂いて色々調べてみたところ下記のようにしたら解消できました! <解決策> 1. produciton.rb内の config.action_mailer.smtp_settings = { の内側に、 config.action_mailer.delivery_method = ENV['delivery_method'](.to_symを抜く)を追記 2. production.rbappliation.ymlに記述されている、.to_symを消す 3.以上の内容をgit add -A, git commit , git pushしてから、git push heroku masterを実施 <原因> 1. smtp認証を使っているが、ymlとproduction.rbが対応していない場所で書かれていた 2._to.symは、シンボルに使うものなので、yml側でシンボルになっているものを使うべきではなかった(消してよかった) かな、と見ています。 しかし、figaro heroku:set -e productionについては、いまだ解決できず、、、 Commented 2020年6月26日 1:14
  • to_sym を抜くと、エラーはでなくなりますが nil のまま保存されているかもしれませんね。
    – nekketsuuu
    Commented 2020年6月26日 1:58
  • 確かに、確認してみないといけないですね。rails consoleで調べられるのでしょうか??初歩的な質問ですみません。 Commented 2020年6月26日 3:19
  • たとえば Heroku の環境で意図した動作が行われているかで確認ができます。staging 環境があれば、staging 環境で puts デバッグするのもアリかもしれません。
    – nekketsuuu
    Commented 2020年6月26日 3:53
  • nekketsuuuさん こちらについて、後ほどやってみますので、こちらの質問閉じようと思います。ありがとうございました! Commented 2020年6月27日 4:31

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。