1

私のOmniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
    def facebook
        # You need to implement the method below in your model (e.g. app/models/user.rb)
        @user ||=
            User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

        if @user.persisted?
            # This will throw if @user is not activated
            sign_in_and_redirect @user, event: :authentication
            if is_navigational_format?
                set_flash_message(:notice, :success, kind: "Facebook")
            end
        else
            session["devise.facebook_data"] = request.env["omniauth.auth"]
            redirect_to new_user_registration_url
        end
    end
end

問題ありません。ログはありません。しかし、誰かが Facebook 経由で接続しようとすると、単にデフォルトの登録フォームにスローされます。

http://localhost:3000/sign_up#_=_

私のユーザーモデル

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable, #:recoverable
         :rememberable, :trackable, :validatable, :omniauthable,
                 :omniauth_providers => [:facebook]


    # Facebook Settings
    def self.find_for_facebook_oauth(auth, signed_in_resource = nil)
        user = User.where(provider: auth.provider, uid: auth.uid).first
        if user.present?
            user
        else
            user = User.create(first_name:auth.extra.raw_info.first_name,
                                                 last_name:auth.extra.raw_info.last_name,
                                                 facebook_link:auth.extra.raw_info.link,
                                                 provider:auth.provider,
                                                 uid:auth.uid,
                                                 email:auth.info.email,
                                                 password:Devise.friendly_token[0,20])
        end
    end



    validates :first_name, :presence => true
    validates :email, :presence => true
    validates :user_name, :presence => true, :uniqueness => true

    has_many :clips
    has_one :show, dependent: :destroy

    # Profile Page Avatar
    has_attached_file :avatar, styles: { avatar: "64x64#"},
                                        :default_url => "http://placehold.it/150x150&text=Missing"

    validates_attachment :avatar,
                                             content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] },
                                             size: { less_than: 5.megabytes }

    # Profile Page Cover
    has_attached_file :profile_cover, styles: { cover: "870x150#"},
                                        :default_url => "http://placehold.it/150x150&text=Missing"

    validates_attachment :profile_cover,
                                             content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] },
                                             size: { less_than: 5.megabytes }


    # For Using the username instead of ID in the Link
    def to_param
        user_name
    end

end
4

1 に答える 1

2

他の情報が関係しているため、理由は100%わかりません。

私は提案します:

  1. この行を変更

    @user ||= User.find_for_facebook_oauth ...
    

    @user = User.find_for_facebook_oauth
    

    これは、考えられる他の影響を排除するためのものであり、このアクションは頻繁にヒットしないため、@user をキャッシュする意味はほとんどありません。

  2. provideruidname列が追加および移行されていることを確認してください。

  3. 理由は @user が永続化されていないため、ユーザーの作成時に何か問題がある可能性があります。に変更createcreate!て、スローされたエラーを確認できます。

    それでもわからない場合は、この User メソッドにデバッグしてください。

アップデート

わかりました、理由がわかりました。検証があります:

validates :user_name, :presence => true, :uniqueness => true

しかし、Facebookの属性にはそのような属性がない:user_nameため、保存に失敗します。

修正は、この属性を Facebook ハッシュに追加する方法を使用するか、この属性を検証しないことです。

于 2013-08-28T06:43:47.120 に答える