2012-08-28 97 views
2

我跟着this tutorial精確,但我收到此錯誤:Omniauth-facebook未定義的方法`slice'爲nil:NilClass。 NoMethodError在SessionsController#創建

NoMethodError in SessionsController#create 
undefined method `slice' for nil:NilClass 
Rails.root: /Users/raybesiga/Documents/Sites/foodie 
Application Trace | Framework Trace | Full Trace 
app/models/user.rb:29:in from_omniauth' 
app/controllers/sessions_controller.rb:3:increate' 

但是我user.rb文件如下:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :name, :email, :password, :password_confirmation, :remember_me,  :provider, :uid 
    validates_presence_of :name 
    validates_uniqueness_of :name, :email, :case_sensitive => false 
    # attr_accessible :title, :body 

    def self.from_omniauth(auth) 
    where(auth.slice(:provider, :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 
end 

sessions_controller.rb文件如下:

class SessionsController < ApplicationController 
     def create 
       user = User.from_omniauth(env["ominauth.auth"]) 
       # session[:user_id] = user.user_id 
       session[:user_id] = user.id 
       redirect_to root_url 
     end 

     def destroy 
       session[:user_id] = nil 
       redirect_to root_url 
     end 
end 

任何想法,爲什麼我得到這個錯誤?

回答

1

您拼寫爲env['omniauth.auth']在您的會話控制器中出錯。你把env['ominauth.auth']

+0

謝謝查爾斯!我怎麼錯過了? – raybesiga

+0

我在自己的應用程序中拼錯了幾次。 –

相關問題