2017-07-18 48 views
0

我一直在努力让Omniauth与多个提供商工作了几天。我终于拥有它(主要是)工作 - Facebook和Twitter按预期工作同步,但是我遇到了Steam的问题。Rails控制器返回undefined方法'保存',但无论如何保存

class AuthenticationsController < ApplicationController 
    skip_before_action :verify_authenticity_token, :only => :steam 

    def index 
    @authentications = current_user.authentications.all 
    end 
    # <%= link_to 'Authentications', authentications_path %> 
    def home 
    end 

    def twitter 
    omni = request.env["omniauth.auth"] 
    authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid']) 

    if authentication 
     flash[:notice] = "Logged in Successfully" 
     sign_in_and_redirect User.find(authentication.user_id) 
    elsif current_user 
     token = omni['credentials'].token 
     token_secret = omni['credentials'].secret 

     current_user.authentications.create!(:provider => omni['provider'], 
              :uid => omni['uid'], 
              :token => token, 
              :token_secret => token_secret) 
     flash[:notice] = "Authentication successful." 
     sign_in_and_redirect current_user 
    else 
     user = User.new 
     user.apply_omniauth(omni) 
     raise user.inspect 
     if user.save 
     flash[:notice] = "Logged in." 
     sign_in_and_redirect User.find(user.id)    
     else 
     session[:omniauth] = omni.except('extra') 
     redirect_to new_user_registration_path 
     end 
    end 
    end 

    def destroy 
    @authentication = Authentication.find(params[:id]) 
    @authentication.destroy 
    redirect_to authentications_url, :notice => "Successfully destroyed authentication." 
    end 


    def facebook 
    omni = request.env["omniauth.auth"] 
    authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid']) 

    if authentication 
     flash[:notice] = "Logged in Successfully" 
     sign_in_and_redirect User.find(authentication.user_id) 
    elsif current_user 
     token = omni['credentials'].token 
     token_secret = "" 

     current_user.authentications.create!(:provider => omni['provider'], 
              :uid => omni['uid'], 
              :token => token, 
              :token_secret => token_secret) 

     flash[:notice] = "Authentication successful." 
     sign_in_and_redirect current_user 
    else 
     user = User.new 
     user.email = omni['extra']['raw_info'].email 

     user.apply_omniauth(omni) 

     if user.save 
     flash[:notice] = "Logged in." 
     sign_in_and_redirect User.find(user.id)    
     else 
     session[:omniauth] = omni.except('extra') 
     redirect_to new_user_registration_path 
     end 
    end 
    end 

    def steam 
     omni = request.env["omniauth.auth"] 
     authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid']) 

     if authentication 
      flash[:notice] = "Logged in Successfully" 
      sign_in_and_redirect User.find(authentication.user_id) 
     elsif current_user 
     token = omni['extra']['raw_info'].steamid 
     # render :text => request.env["omniauth.auth"].info.to_hash.inspect 

     puts token 
     token_secret = "" 

      current_user.authentications.create!(:provider => omni['provider'], 
               :uid => omni['uid'], 
               :token => token, 
               :token_secret => token_secret) 
      flash[:notice] = "Authentication successful." 
      sign_in_and_redirect current_user 
     else 
      user = User.new 
      user.apply_omniauth(omni) 
     end 

     if user.save 
      flash[:notice] = "Logged in." 
      sign_in_and_redirect User.find(user.id)    
     else 
      session[:omniauth] = omni.except('extra') 
      redirect_to new_user_registration_path 
     end 
    end 




end 

正如你可以看到twitter功能和steam几乎是重复的 - 他们引用User model这是内部的功能

def apply_omniauth(omni) 
    authentications.build(:provider => omni['provider'], 
          :uid => omni['uid'], 
          :token => omni['credentials'].token, 
          :token_secret => omni['credentials'].secret) 
    end 

目前汽将与undefined method 'save' for nil 上回应

if user.save 
    flash[:notice] = "Logged in." 
    sign_in_and_redirect User.find(user.id)    
else 

让我感到困惑的是它实际上是储蓄。

=> #<Authentication id: 12, provider: "steam", uid: "redacted", token: "76561198038103683", token_secret: "", created_at: "2017-07-18 14:57:32", updated_at: "2017-07-18 14:57:32", user_id: 2> 
2.4.1 :022 > u.authentications.last.destroy 
    Authentication Load (0.8ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."user_id" = $1 ORDER BY "authentications"."id" DESC LIMIT $2 [["user_id", 2], ["LIMIT", 1]] 
    (0.3ms) BEGIN 
    SQL (0.6ms) DELETE FROM "authentications" WHERE "authentications"."id" = $1 [["id", 13]] 
    (4.2ms) COMMIT 
=> #<Authentication id: 13, provider: "steam", uid: "redacted", token: "redacted", token_secret: "", created_at: "2017-07-18 15:13:00", updated_at: "2017-07-18 15:13:00", user_id: 2> 
2.4.1 :023 > u.authentications.last 
    Authentication Load (0.6ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."user_id" = $1 ORDER BY "authentications"."id" DESC LIMIT $2 [["user_id", 2], ["LIMIT", 1]] 
=> #<Authentication id: 14, provider: "steam", uid: "redacted", token: "redacted", token_secret: "", created_at: "2017-07-18 15:13:19", updated_at: "2017-07-18 15:13:19", user_id: 2> 

我删除记录 - 然后去重新授权,并获得相同的错误,但关系确实存在。

我不确定如何打印出user以查看它发生了什么,或者如何在此内部进行错误测试。

欢迎任何反馈,对于这个宝石已经很长时间了。

回答

0

所以你设置user变量只在else分支。如果任何其他分支被评估user变量为零。只是移动最后如果(if user.save)为else支路的第二如果:

def steam 
    omni = request.env["omniauth.auth"] 
    authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid']) 

    if authentication 
     flash[:notice] = "Logged in Successfully" 
     sign_in_and_redirect User.find(authentication.user_id) 
    elsif current_user 
    token = omni['extra']['raw_info'].steamid 
    # render :text => request.env["omniauth.auth"].info.to_hash.inspect 

    puts token 
    token_secret = "" 

     current_user.authentications.create!(:provider => omni['provider'], 
              :uid => omni['uid'], 
              :token => token, 
              :token_secret => token_secret) 
     flash[:notice] = "Authentication successful." 
     sign_in_and_redirect current_user 
    else 
     user = User.new 
     user.apply_omniauth(omni) 
     if user.save 
      flash[:notice] = "Logged in." 
      sign_in_and_redirect User.find(user.id)    
     else 
      session[:omniauth] = omni.except('extra') 
      redirect_to new_user_registration_path 
     end 
    end 
end 
+0

我没有想到会这样,但我得到它。我所苦苦挣扎的,从技术上讲不应该击中那个'别的'。当它同步我时,它会触及'elsif current_user',然后继续下去。我对轨道是新的,但我不明白为什么它在做elsif,然后也在做别的。 – DNorthrup

+0

你没有做'else' :)你正在做下一个'if',因为'sign_in_and_redirect'不会停止执行代码的其余部分。如果在下一个方法之后添加'return',如果不执行。 – Ptr

+0

感谢您的详细信息。我有种,但不完全。我尝试了'else'之上的返回,如果user.save在上面,我仍然在user.save上发生错误 - 你能告诉我一个你想要的例子吗? – DNorthrup

相关问题