简体   繁体   中英

Omniauth-facebook undefined method `slice' for nil:NilClass. NoMethodError in SessionsController#create

I followed this tutorial precisely but I am getting this error:

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'

However my user.rb file is as below:

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

My sessions_controller.rb file is as follows:

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

Any idea why I get this error?

You spelled env['omniauth.auth'] wrong in your sessions controller. You put env['ominauth.auth']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM