NoMethodError(未定义的方法`持久化?'为true:TrueClass)

时间:2015-10-15 00:41:22

标签: ruby-on-rails devise omniauth

我正在实现rails webapp,在这个应用程序中我使用Devise + Omniauth进行身份验证。在我的Devise模型中,我也使用了可确认的块。

现在当我尝试通过omniauth为facebook或google实现社交登录时,我收到以下错误

  NoMethodError at /customer/auth/google_oauth2/callback
  undefined method `persisted?' for true:TrueClass

以下是我的omniauth_callbacks控制器的代码

    class OmniauthCallbacksController < Devise::OmniauthCallbacksController

    skip_before_filter :authenticate_user!
    def all
        p env["omniauth.auth"]
        user = User.from_omniauth(env["omniauth.auth"], current_user)
        if user.persisted?
            flash[:notice] = "You are in..!!! Go to edit profile to see the status for the accounts"
            sign_in_and_redirect(user)
        else
            session["devise.user_attributes"] = user.attributes
            redirect_to new_user_registration_url
        end
    end

      def failure      
        super
      end


    alias_method :facebook, :all
    alias_method :google_oauth2, :all
end

这是我的用户模型的代码段

class User&lt;的ActiveRecord ::基

devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable,:confirmable,:token_authenticatable

def self.from_omniauth(auth, current_user)    
    authorization = Authorization.where(:provider => auth.provider, 
                    :uid => auth.uid.to_s, 
                    :token => auth.credentials.token, 
                    :secret => auth.credentials.secret).first_or_initialize

    if authorization.user.blank?
        user = current_user || User.where('email = ?', auth["info"]["email"]).first        
        if user.blank?       
          user = User.new
          user.name = auth.info.name
          user.email= auth.info.email
          if auth.provider == "twitter" 
            user.save(:validate => false) 
          else
            user.skip_confirmation!
            user.save(:validate => false)
          end
        end
        authorization.username = auth.info.nickname
        authorization.user_id = user.id
        authorization.save
    end
    authorization.user
end

我的用户模型有许多授权模型(创建用于处理来自多个提供商的帐户)

class Authorization < ActiveRecord::Base
    belongs_to :user

    after_create :fetch_details



    def fetch_details
        self.send("fetch_details_from_#{self.provider.downcase}")
    end


    def fetch_details_from_facebook
        graph = Koala::Facebook::API.new(self.token)
        facebook_data = graph.get_object("me")
        self.username = facebook_data['username']
        self.save
    end
 end

任何人都可以帮我找到我为什么会这样做      未定义的方法`坚持?'为true:TrueClass

错误?

1 个答案:

答案 0 :(得分:0)

$ perl -le 'print length("\320\300\304\310\323\321 \316\320\300\312\313")' 12 $ perl -le 'print length($ARGV[0])' "\320\300\304\310\323\321 \316\320\300\312\313" 45 助手在omniauth系列中扮演什么角色?我假设它与omniauth环境变量产生某种冲突。

尝试更改:

current_user

为:

user = User.from_omniauth(env["omniauth.auth"], current_user)