![](/img/trans.png)
[英]Rails 3.* Devise Facebook OmniAuth intermittently fails with NoMethodError
[英]NoMethodError with Devise and Omniauth -rails
我正在嘗試將Omniauth Facebook登錄名與Devise集成在一起,以開發我目前處於開發初期的Rails應用程序。 我是一個(非常)初級開發人員,遇到了一個障礙,我不確定該如何解決。
到目前為止,我已經成功安裝了Devise gem,並成功使Omniauth Facebook能夠正常工作到使用Facebook登錄該應用程序(通過遵循https://github.com/plataformatec/devise上的文檔) / wiki / OmniAuth:-Overview )-但是,在重定向回我的應用程序時,顯示以下錯誤消息:
用戶中的NoMethodError :: OmniauthCallbacksController#facebook未定義的方法`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
我希望這足以幫助我診斷問題,盡管請告知我是否需要其他代碼。 提前非常感謝!
您的用戶模型是否有名稱和圖像? 嘗試刪除以下兩行:
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.