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
任何想法,爲什麼我得到這個錯誤?
謝謝查爾斯!我怎麼錯過了? – raybesiga
我在自己的應用程序中拼錯了幾次。 –