我正在尝试将Omniauth Facebook登录与Devise集成到Rails应用程序中,我目前正处于开发的早期阶段。我是一个(非常)初级的开发人员,遇到了一个不确定如何解决的障碍。
到目前为止,我已经成功地安装了Devise gem,并成功地让Omniauth Facebook运行到使用Facebook登录应用程序的地步(通过以下文档https://github.com/plataformatec/devise/wiki/OmniAuth:-概述)-然而,在重定向回我的应用程序时,显示以下错误消息:
用户中的NoMethodError::OmniauthCallbacksController#facebook的未定义方法"name="#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
我希望这足以帮助我诊断问题,但如果需要任何进一步的代码,请告诉我。非常感谢!
您的用户模型有名称和图像吗?尝试删除这两行:
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image