title: Rails / Devise: undefined method `router_name' for nil:NilClass - what is coming through as nil?
body: <p>So this code (Devise &amp; OAuth2 in Rails 5)</p>
  
  <pre><code>class Users::OmniauthCallbacksController &lt; Devise::OmniauthCallbacksController
      def google_oauth2
          puts request.env['omniauth.auth']
          @user = User.from_omniauth(request.env['omniauth.auth'])
  
          if @user.persisted?
              sign_in_and_redirect root_path, event: :authentication # &lt;--- THIS LINE IS THE CULPRIT
          else
              redirect_to root_path, flash: { error: 'Authentication failed!' }
          end
      end
  end
  </code></pre>
  
  <p>tells me</p>
  
  <pre><code>undefined method `router_name' for nil:NilClass
  </code></pre>
  
  <p>How do I go about tracking this down?</p>
  
  <p>What exactly is it that is <strong>nil</strong> at this stage?</p>
  
  <p>I got no enlightenment doing the following...</p>
  
  <p><a href="https://i.stack.imgur.com/eHF93.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eHF93.jpg" alt=""></a>
  </p>
  
  <p>and the server log...</p>
  
  <pre><code>Started GET "/users/auth/google_oauth2" for ::1 at 2016-07-22 19:06:28 -0400
  I, [2016-07-22T19:06:28.730884 #5714]  INFO -- omniauth: (google_oauth2) Request phase initiated.
  Started GET "/users/auth/google_oauth2/callback?state=45315985daa3339fe9fa10f3e57dedaadfc4f4aa60f06f06&amp;code=4/I8O7qeJSR--qeDdep-Iwf1EkdI--SlAj2KWJz7MGCpE" for ::1 at 2016-07-22 19:06:34 -0400
  I, [2016-07-22T19:06:34.760625 #5714]  INFO -- omniauth: (google_oauth2) Callback phase initiated.
  Processing by Users::OmniauthCallbacksController#google_oauth2 as HTML
    Parameters: {"state"=&gt;"45315985daa3339fe9fa10f3e57dedaadfc4f4aa60f06f06", "code"=&gt;"4/I8O7qeJSR--qeDdep-Iwf1EkdI--SlAj2KWJz7MGCpE"}
    User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT $3  [["provider", "google_oauth2"], ["uid", "106038339500381304171"], ["LIMIT", 1]]
  Completed 500 Internal Server Error in 17ms (ActiveRecord: 4.5ms)
  
  
  
  NoMethodError (undefined method `router_name' for nil:NilClass):
  
  app/controllers/users/omniauth_callbacks_controller.rb:6:in `google_oauth2'
    Rendering /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
    Rendering /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
    Rendered /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (10.3ms)
    Rendering /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
    Rendered /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.1ms)
    Rendering /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
    Rendered /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
    Rendered /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (98.5ms)
  </code></pre>