Facebook omniauth nil object

Rails 3.1.3

gem 'authbuttons-rails'
gem 'twitter'
gem 'omniauth-twitter'
gem 'omniauth-facebook'

My app needs Facebook integration, and the similar method works for
Twitter API.
When it comes to Facebook, the create method in services_controller.rb
raises an error

  You have a nil object when you didn't expect it!
  You might have expected an instance of Array.
  The error occurred while evaluating nil.[]

  app/models/user.rb:47:in `connect_service'
  app/controllers/services_controller.rb:18:in `create'

Sorry about the length of the code.

Do you see any problem? I believe that I'm making a stupid mistake
because the code got too long...

# and connect_service method in user.rb is

    def connect_service(omniauth)
        #set uid and provider
        case omniauth['provider']
        when 'twitter'
            omniauth['uid'] ? uid = omniauth['uid'] : uid = ''
            omniauth['provider'] ? provider = omniauth['provider'] :
provider = ''
            token = (omniauth['credentials']['token'] rescue nil)
            secret = (omniauth['credentials']['secret'] rescue nil)
        when 'facebook'
            omniauth['extra']['user_hash']['id'] ? uid =
omniauth['extra']['user_hash']['id'] : uid = '' ###HERE!!
            omniauth['provider'] ? provider = omniauth['provider'] :
provider = ''
            token = (omniauth['credentials']['token'] rescue nil)
            secret = ''
        services.build(:provider => provider, :uid => uid, :token =>
token, :secret => secret)

at line 18 where I indicated by putting "###HERE!" in the code
# service controller that creates Twitter and Facebookl things

class ServicesController < ApplicationController
    before_filter :authenticate_user!, :except => [:create]

    def index
        # get all authentication services assigned to the current user
        @services = current_user.services.all

    def create
        # get the full hash from omniauth
        omniauth = request.env['omniauth.auth']
        authentication =
Service.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
        if authentication.present?
            flash[:notice] = "Signed in successfully."
        elsif omniauth.present?
            if user_signed_in?
                if current_user.save
                    redirect_to services_url, notice: "Authentication
                    redirect_to root_path, alert: "Error while saving
Authentication information."
            elsif (omniauth['provider'] == "facebook")
                if existinguser =
User.find_by_email(omniauth['extra']['user_hash']['email']) ####HERE!
                    if existinguser.save
                        flash[:notice] = "Signed in successfully."
                        redirect_to root_path, alert: "Error while
saving Authentication information."
                    session[:omniauth] = omniauth
                    redirect_to new_user_registration_url
                session[:omniauth] = omniauth.except('extra')
                redirect_to new_user_registration_url
            redirect_to root_path, alert: "Error while authenticating."

    def destroy
        # remove an authentication service linked to the current user
        @service = current_user.services.find_by_id(params[:id])

        if @service.present?
            redirect_to services_path, notice: 'Service was successfully
            redirect_to services_path, alert: 'Service was not found.'


I presume that you meant line 47 in user.rb rather than 18.
That suggests that omniauth['extra'] or omniauth['extra']['user_hash']
is nil. Have a look at the Rails Guide on debugging for ideas on how
to debug this. You could break in here with the debugger or put some
extra tests in the code with puts statements to show the result.


thanks for your answer.
I finally managed to find the problem.

omniauth['extra']['user_hash']['id'] ? uid =
omniauth['extra']['user_hash']['id'] : uid = ''

should be

omniauth['info']['id'] ? uid =
omniauth['info']['id'] : uid = ''

of course, all the other

"['extra']['user_hash']" parts are now "['info']" due to API changes.