我正在尝试将 Omniauth Facebook 登录与 Devise 集成到我目前处于开发早期阶段的 Rails 应用程序中。我是一个(非常)初级开发人员,我遇到了一个我不确定如何解决的障碍。
到目前为止,我已经成功地安装了 Devise gem,并成功地让 Omniauth Facebook 工作到使用 Facebook 登录应用程序的程度(通过遵循https://github.com/plataformatec/devise上的文档/wiki/OmniAuth:-Overview ) - 但是,在重定向回我的应用程序时,会显示以下错误消息:
NoMethodError in Users::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
我希望这足以帮助我诊断问题,但如果需要任何进一步的代码,请告诉我。非常感谢提前!