2014-11-22 28 views
0

我想實現Facebook的登錄身份驗證使用devise和omniauth,但我在回調部分中出現錯誤。未定義的本地變量或方法`用戶'omniauth回調控制器

的錯誤是這樣的......

> `User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."provider" = ? AND "users"."uid" = ? ORDER BY "users"."id" ASC LIMIT 1 [["provider", "facebook"], ["uid", "1517802078458724"]] 
    (0.1ms) begin transaction 
    (0.1ms) rollback transaction 
Completed 500 Internal Server Error in 411ms 

NameError (undefined local variable or method `user' for #<User:0x007f3008b8a760>): 
    app/models/user.rb:34:in `password_required?' 
    app/models/user.rb:18:in `block in from_omniauth' 
    app/models/user.rb:12:in `tap' 
    app/models/user.rb:12:in `from_omniauth' 
    app/controllers/omniauth_callbacks_controller.rb:4:in `all'` 

這似乎是工作的事情,直到/認證/ Facebook的/,然後上升到檢索部分的UID和供應商結束。

我在控制器代碼,型號和路線如下..

回調控制器和應用控制器

class OmniauthCallbacksController < Devise::OmniauthCallbacksController 

    def all 
    user = User.from_omniauth(request.env["omniauth.auth"]) 
    if user.persisted? 
     sign_in_and_redirect user, notice: "Signed in!" 
    else 
     session["devise.user_attributes"] = user.attributes 
     redirect_to new_user_registration_url 
    end 
    end 
    alias_method :facebook, :all 
end 


class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 

end 

我user.rb模型

class User < ActiveRecord::Base 
    has_many :authentications 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, :omniauthable, 
     :recoverable, :rememberable, :trackable, :validatable, 
     :omniauth_providers => [:facebook] 

    attr_accessible :email, :password, :password_confirmation 

    def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user| 
     user.provider = auth.provider 
     user.uid = auth.uid 
     user.name = auth.info.name 
     user.oauth_token = auth.credentials.token 
     user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
     user.save! 
    end 
    end 

    def self.new_with_sessions(params, session) 
    if session["devise.user_attributes"] 
     new(session["devise.user_attributes"], without_protection: true) do |user| 
     user.attributes = params 
     user.valid? 
     end 
    else 
     super 
    end 
    end 

    def password_required? 
    super && user.blank? 

    end 
end 

我schema.rb

ActiveRecord :: Schema.define(version:20141120211712)do

create_table "users", force: true do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "provider" 
    t.string "uid" 
    t.string "name" 
    t.string "oauth_token" 
    t.datetime "oauth_expires_at" 
    end 

    add_index "users", ["email"], name: "index_users_on_email", unique: true 
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 

end 

我的routes.rb

Rails.application.routes.draw do 

    devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" } 
    root   'static_pages#home' 
    get 'help' => 'static_pages#help' 
    get 'about' => 'static_pages#about' 
    get 'contact' => 'static_pages#contact' 

,最後我的Gemfile

source 'https://rubygems.org' 
ruby '2.1.4' 

gem 'rails',     '4.2.0.beta4' 
gem 'bcrypt',     '3.1.7' 
gem 'faker',     '1.4.2' 
gem 'carrierwave',    '0.10.0' 
gem 'mini_magick',    '3.8.0' 
gem 'fog',      '1.23.0' 
gem 'will_paginate',   '3.0.7' 
gem 'bootstrap-will_paginate', '0.0.10' 
gem 'bootstrap-sass',   '3.2.0.0' 
gem 'sass-rails',    '5.0.0.beta1' 
gem 'uglifier',    '2.5.3' 
gem 'coffee-rails',   '4.0.1' 
gem 'jquery-rails',   '4.0.0.beta2' 
gem 'turbolinks',    '2.3.0' 
gem 'jbuilder',    '2.2.3' 
gem 'rails-html-sanitizer', '1.0.1' 
gem 'sdoc',     '0.4.0', group: :doc 
gem 'devise', github: 'plataformatec/devise' 
gem 'omniauth' 
gem 'omniauth-twitter' 
gem 'omniauth-facebook' 
gem 'omniauth-linkedin' 
gem 'figaro' 
gem 'protected_attributes' 

我真的想Facebook登錄在我的應用程序這個功能...預先感謝您非常。我會非常感激任何輸入..

回答

1

問題是在password_required?這是保存用戶時調用。

def password_required? 
    super && user.blank? 

end 

它試圖測試user是否爲空?但是,用戶變量/方法不在其範圍內。相反,您需要使用指向當前用戶實例的self

所以,該行應爲:

super && self.blank? 

或者只是:

super && blank? 
+0

現在我得到ActiveRecord :: RecordInvalid(驗證失敗:電子郵件不能爲空):錯誤。我現在該做什麼? – ahk 2014-11-22 13:34:06

0

我能解決這個問題throughmaking

def password_required? 
    super && user.blank? 

end 

super && self.blank? 

和我還添加

user.email = auth.info.email 

self.from_omniauth(auth)部。 謝謝你們

相關問題