0

在我的 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
4

1 回答 1

1

你的问题在这一行......

用户 = User.authorizations.from_auth(auth_hash)

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

于 2013-05-15T03:18:42.673 回答