2016-07-22 101 views
2

所以這個代碼(設計&的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) 
+0

你能發佈相關的日誌請求和錯誤嗎? –

+0

什麼是'nil'是具有'router_name'方法的對象。找到填充該字段的代碼位。 – MarsAtomic

+0

它應該是'sign_in_and_redirect @ user' –

回答

0

嘗試改變: sign_in_and_redirect root_path, event: :authentication

sign_in_and_redirect root_path, event: => :authentication

+0

我認爲你的意思是:event =>:authentication ...但是,謝謝 – dwilbank

+0

這只是Ruby 1.8的語法,它會導致執行完全相同的代碼。 –

2

只需更改sign_in_and_redirect root_pathsign_in(:user, @user)

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(:user, @user), event: :authentication 
     else 
      sign_in(:user, @user), flash: { error: 'Authentication failed!' } 
     end 
    end 
end 

希望這對你有所幫助。

+0

感謝您的建議。 sign_in(:user,@user)被標記爲不良語法。儘管sign_in_and_redirect @user工作。仍然不知道'router_name'在哪裏出現......魔術紅寶石在工作中...... – dwilbank

+0

它能幫助你嗎? – VKatz

+0

我會豎起大拇指,但問題仍然存在。謝謝 – dwilbank

相關問題