RailsCasts Pro episodes are now free!

Learn more or hide this

Ray Besiga's Profile

GitHub User: raybesiga

Site: http://raybesiga.com

Comments by Ray Besiga

Avatar

Hey guys,

I followed the tutorial precisely but I am getting an error such as this:

NoMethodError in SessionsController#create

ruby
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:in
create'

However my user.rb file is as below:

ruby
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

and my sessions_controller.rb file is as follows:

ruby
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?