現在、開発の初期段階にあるRailsアプリケーションのOmniauth FacebookログインをDeviseと統合しようとしています。私は (非常に) 経験の浅い開発者ですが、解決方法がわからない障害にぶつかりました。
これまでのところ、Devise gem のインストールに成功し、Facebook でアプリにログインするまで Omniauth Facebook を機能させることができました ( https://github.com/plataformatec/deviseのドキュメントに従ってください)。 /wiki/OmniAuth:-Overview ) - ただし、アプリにリダイレクトすると、次のエラー メッセージが表示されます。
ユーザーの NoMethodError::OmniauthCallbacksController#facebook undefined method `name=' for # user.name = auth.info.name # ユーザー モデルに名前があると仮定
問題の解決に役立つと思われるコード スニペットは次のとおりです。
私のユーザーモデル:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
end
end
end
私のルートファイル:
Rails.application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
root to: "restaurants#index"
devise_scope :user do
get 'sign_out', :to => 'devise/sessions#destroy', only: :destroy_user_session
end
resources :restaurants do
resources :reviews
end
end
問題を診断するのにこれで十分だと思いますが、さらにコードが必要な場合はお知らせください。よろしくお願いします!