0

私の Rails アプリには、ユーザーと認証データを処理するテーブルがありますuserauthorizationTwitter を使用してサインアップするように Devise と Omniauth の両方を設定すると、Twitter にリダイレクトされますが、アプリに戻った後、次のようなエラーが表示されます。

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

どちらの側で間違っていましたか?どうすればこの問題を解決できますか?

Here are related parts: 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

Your issue is in this line...

user = User.authorizations.from_auth(auth_hash)

You call authorizations on the class User, but as an attribute it needs to be called on an instance of the User class, i.e. a specific user.

于 2013-05-15T03:18:42.673 に答える