Rails,Omniauth:NoMethodError - 未定义的方法`持久化?'

时间:2017-01-16 06:07:36

标签: ruby-on-rails ruby omniauth ruby-on-rails-5 stripe-connect

我正在使用omniauth-stripe-connect gem并在对用户进行身份验证时收到以下错误:

  用户中的 NoMethodError :: OmniauthCallbacksController #spap_connect   未定义的方法`坚持?'为零:NilClass

我的回调控制器动作:

def stripe_connect
    @user = UserProvider.find_for_stripe_oauth(request.env["omniauth.auth"])
    if @user.persisted? # here is the issue !!!
        sign_in @user, :event => :authentication
        redirect_to root_path
    end
end

我的用户方法:

def self.find_for_stripe_oauth(auth)
    user = UserProvider.where(:provider => auth.provider, :uid => auth.uid).first
    unless user.nil?
        user.user
    else
        registered_user = User.where(:email => auth.info.email).first
        unless registered_user.nil?
            user_provider = UserProvider.new
            user_provider.provider = auth.provider
            user_provider.uid = auth.uid
            user_provider.user_id = registered_user.id
            user_provider.save!

            registered_user.stripe_access_code = auth.credentials.token
            registered_user.stripe_publishable_key = auth.info.stripe_publishable_key
            registered_user.stripe_id = auth.extra.raw_info.stripe_user_id
            registered_user.skip_confirmation!
            registered_user.save!

            registered_user
        end
    end
end

我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果find_for_stripe_oauth不存在,

nil会返回registered_user。在这种情况下,unless registered_user.nil?返回false并且不执行内部块。

移出registered_user,如下所示:

    registered_user = User.where(:email => auth.info.email).first
    unless registered_user.nil?
        user_provider = UserProvider.new
        user_provider.provider = auth.provider
        user_provider.uid = auth.uid
        user_provider.user_id = registered_user.id
        user_provider.save!

        registered_user.stripe_access_code = auth.credentials.token
        registered_user.stripe_publishable_key = auth.info.stripe_publishable_key
        registered_user.stripe_id = auth.extra.raw_info.stripe_user_id
        registered_user.skip_confirmation!
        registered_user.save!
    end

    registered_user