简体   繁体   中英

Rails 5 - Devise - Heroku NoMethodError

I am trying to deploy a simple Rails 5 application in Heroku which has devise authentication enabled. I have followed a tutorial to deploy the Rails app on Heroku. The app is running and the database is also setup.

When I try to load the initial page of the app, where the user must be redirected to login page, I am getting following exception (in Heroku logs) -

 Started GET "/" for 203.187.254.82 at 2018-05-04 10:28:24 +0000
 Processing by HomeController#index as HTML
 Completed 401 Unauthorized in 1ms

 NoMethodError (undefined method `merge' for nil:NilClass):

 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/route_set.rb:805:in `url_for'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/route_set.rb:277:in `call'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/route_set.rb:218:in `call'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/route_set.rb:335:in `block (2 levels) in define_url_helper'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/routing/routes_proxy.rb:42:in `new_user_session_url'
 vendor/bundle/ruby/2.3.0/gems/devise-4.4.3/lib/devise/failure_app.rb:153:in `scope_url'
 vendor/bundle/ruby/2.3.0/gems/devise-4.4.3/lib/devise/failure_app.rb:128:in `redirect_url'
 vendor/bundle/ruby/2.3.0/gems/devise-4.4.3/lib/devise/failure_app.rb:88:in `redirect'
 vendor/bundle/ruby/2.3.0/gems/devise-4.4.3/lib/devise/failure_app.rb:43:in `respond'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/abstract_controller/base.rb:186:in `process_action'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/abstract_controller/base.rb:124:in `process'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_controller/metal.rb:189:in `dispatch'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_controller/metal.rb:242:in `block in action'
 vendor/bundle/ruby/2.3.0/gems/devise-4.4.3/lib/devise/failure_app.rb:23:in `call'
 vendor/bundle/ruby/2.3.0/gems/devise-4.4.3/lib/devise/delegator.rb:7:in `call'
 vendor/bundle/ruby/2.3.0/gems/warden-1.2.7/lib/warden/manager.rb:143:in `call_failure_app'
 vendor/bundle/ruby/2.3.0/gems/warden-1.2.7/lib/warden/manager.rb:129:in `process_unauthenticated'
 vendor/bundle/ruby/2.3.0/gems/warden-1.2.7/lib/warden/manager.rb:44:in `call'
 vendor/bundle/ruby/2.3.0/gems/rack-2.0.5/lib/rack/etag.rb:25:in `call'
 vendor/bundle/ruby/2.3.0/gems/rack-2.0.5/lib/rack/conditional_get.rb:25:in `call'
 vendor/bundle/ruby/2.3.0/gems/rack-2.0.5/lib/rack/head.rb:12:in `call'
 vendor/bundle/ruby/2.3.0/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:232:in `context'
 vendor/bundle/ruby/2.3.0/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:226:in `call'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/cookies.rb:613:in `call'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
 vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:97:in `run_callbacks'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/callbacks.rb:24:in `call'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
 vendor/bundle/ruby/2.3.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:36:in `call_app'
 vendor/bundle/ruby/2.3.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `block in call'
 vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:69:in `block in tagged'
 vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:26:in `tagged'
 vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:69:in `tagged'
 vendor/bundle/ruby/2.3.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `call'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/request_id.rb:25:in `call'
 vendor/bundle/ruby/2.3.0/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
 vendor/bundle/ruby/2.3.0/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
 vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.6/lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/executor.rb:12:in `call'
 vendor/bundle/ruby/2.3.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/static.rb:125:in `call'
 vendor/bundle/ruby/2.3.0/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
 vendor/bundle/ruby/2.3.0/gems/railties-5.1.6/lib/rails/engine.rb:522:in `call'
 vendor/bundle/ruby/2.3.0/gems/puma-3.11.4/lib/puma/configuration.rb:225:in `call'
 vendor/bundle/ruby/2.3.0/gems/puma-3.11.4/lib/puma/server.rb:632:in `handle_request'
 vendor/bundle/ruby/2.3.0/gems/puma-3.11.4/lib/puma/server.rb:446:in `process_client'
 vendor/bundle/ruby/2.3.0/gems/puma-3.11.4/lib/puma/server.rb:306:in `block in run'
 vendor/bundle/ruby/2.3.0/gems/puma-3.11.4/lib/puma/thread_pool.rb:120:in `block in spawn_thread'

home_controller.rb

class HomeController < ApplicationController
before_action :authenticate_user!

  def index
    @activities = nil
  end

  def posts
    @posts = Post.all.order("created_at DESC")
  end
end

What am I missing here? Not able to understand whether the exception is from the devise library or my application code.

I have resolved it. I found it from actionpack 's code that url_for method requires default_url_options config to be set in respective environment file.

In my case it was in production.rb file.

I had to put-in -

config.action_mailer.default_url_options = { :host => '<YOUR_HEROKU_APP_URL_HERE>' }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM