所以這個代碼(設計&的OAuth2 Rails中5)Rails/Devise:未定義的方法`router_name'爲nil:NilClass - 什麼是通過爲零?
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
告訴我
undefined method `router_name' for nil:NilClass
我怎麼去追蹤下來?
到底什麼是無在這個階段?
我沒有啓蒙執行以下操作...
和服務器日誌...
Started GET "https://stackoverflow.com/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 "https://stackoverflow.com/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/[email protected]/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /Users/davidwilbanks/.rvm/gems/[email protected]/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /Users/davidwilbanks/.rvm/gems/[email protected]/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (10.3ms)
Rendering /Users/davidwilbanks/.rvm/gems/[email protected]/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /Users/davidwilbanks/.rvm/gems/[email protected]/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.1ms)
Rendering /Users/davidwilbanks/.rvm/gems/[email protected]/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /Users/davidwilbanks/.rvm/gems/[email protected]/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
Rendered /Users/davidwilbanks/.rvm/gems/[email protected]/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (98.5ms)
你能發佈相關的日誌請求和錯誤嗎? –
什麼是'nil'是具有'router_name'方法的對象。找到填充該字段的代碼位。 – MarsAtomic
它應該是'sign_in_and_redirect @ user' –