与Devise和Omniauth -rails的NoMethodError

时间:2014-11-03 19:23:53

标签: ruby-on-rails devise omniauth omniauth-facebook

我正在尝试将Omniauth Facebook登录与Devise整合到一个Rails应用程序中,我目前处于开发的早期阶段。我是(非常)初级开发人员,我遇到了一个障碍,我不确定如何解决。

到目前为止,我已成功安装了Devise gem,并设法让Omniauth Facebook正在努力登录使用Facebook的应用程序(通过https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview处的文档) - 但是,在重定向回我的应用程序时,显示以下错误消息:

  用户中的 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

我希望这足以帮助我诊断问题,但如果需要进一步的代码,请告诉我。非常感谢提前!

1 个答案:

答案 0 :(得分:0)

您的用户模型是否有名称和图片?尝试删除这两行:

user.name = auth.info.name   # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image