Rails / Devise:nil:NilClass 的未定义方法`router_name' - 什么是 nil?

dwi*_*ank 5 ruby ruby-on-rails devise oauth-2.0

所以这段代码(Rails 5 中的设计和 OAuth2)

class Users::OmniauthCallbacksController < 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 # <--- THIS LINE IS THE CULPRIT
        else
            redirect_to root_path, flash: { error: 'Authentication failed!' }
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

告诉我

undefined method `router_name' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)

我该如何追踪?

在这个阶段到底什么是

我在做以下事情时没有得到启发......

和服务器日志...

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&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"=>"45315985daa3339fe9fa10f3e57dedaadfc4f4aa60f06f06", "code"=>"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)
Run Code Online (Sandbox Code Playgroud)

jos*_*osh -1

尝试改变: sign_in_and_redirect root_path, event: :authentication

sign_in_and_redirect root_path, event: => :authentication