【问题标题】:Omniauth callback processing after authorization授权后的Omniauth回调处理
【发布时间】:2013-05-15 02:45:11
【问题描述】:

在我的 Rails 应用程序中,我有 userauthorization 表来处理用户和身份验证数据。我将 Devise 和 Omniauth 都设置为使用 Twitter 注册,它重定向到 Twitter,但是在返回我的应用程序后,它给出了如下错误:

NoMethodError at /users/auth/twitter/callback 
undefined method `authorizations' for #<Class:0xbdc8100>

我在哪方面做错了,我该如何解决这个问题?

这里是相关部分:omniauth_callbacks_controller.rb:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
    def all
        user = User.authorizations.from_auth(auth_hash)
        if user.persisted?
            flash.notice = "Signed in!"
            sign_in_and_redirect user
        else
            session["devise.user_attributes"] = user.attributes
            redirect_to new_user_registration_url
        end
    end


    alias_method :twitter, :all

    protected
    def auth_hash
        request.env['omniauth.auth']
    end
end

authorization.rb:

class Authorization < ActiveRecord::Base

    attr_accessible :uid, :provider
    belongs_to :user

    def self.from_auth(auth)
        where(auth.slice(:provider, :uid)).first_or_create do |user|
        user.provider = auth.provider
        user.uid = auth.uid
    end
end

user.rb:

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:twitter, :facebook]

# Setup accessible (or protected) attributes for your model
    attr_accessible :email, :password, :password_confirmation, :remember_me, :name
# attr_accessible :title, :body

    has_many :authorizations, dependent: :destroy

end

【问题讨论】:

  • 我认为你应该使用 Authorization.from_auth 而不是 User.authorizations.from_auth
  • 我之前已经尝试过Authorization.from_auth,但也没有用。

标签: ruby-on-rails ruby devise omniauth twitter-oauth


【解决方案1】:

您的问题在这一行...

user = User.authorizations.from_auth(auth_hash)

您在 User 类上调用授权,但作为属性,它需要在 User 类的实例(即特定用户)上调用。

【讨论】:

  • 我明白了,它说明了问题,但我也需要修复它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多