Rails 3.1.3
Gemfile
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 = ''
end
services.build(:provider => provider, :uid => uid, :token =>
token, :secret => secret)
end
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
end
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."
sign_in_and_redirect(:user,authentication.user)
elsif omniauth.present?
if user_signed_in?
current_user.connect_service(omniauth)
if current_user.save
redirect_to services_url, notice: "Authentication
successful."
else
redirect_to root_path, alert: "Error while saving
Authentication information."
end
elsif (omniauth['provider'] == "facebook")
if existinguser =
User.find_by_email(omniauth['extra']['user_hash']['email']) ####HERE!
existinguser.connect_service(omniauth)
if existinguser.save
flash[:notice] = "Signed in successfully."
sign_in_and_redirect(:user,existinguser)
else
redirect_to root_path, alert: "Error while
saving Authentication information."
end
else
session[:omniauth] = omniauth
redirect_to new_user_registration_url
end
else
session[:omniauth] = omniauth.except('extra')
redirect_to new_user_registration_url
end
else
redirect_to root_path, alert: "Error while authenticating."
end
end
def destroy
# remove an authentication service linked to the current user
@service = current_user.services.find_by_id(params[:id])
if @service.present?
@service.destroy
redirect_to services_path, notice: 'Service was successfully
deleted.'
else
redirect_to services_path, alert: 'Service was not found.'
end
end
end
soichi
on 2012-11-30 02:32
on 2012-11-30 10:02
On 30 November 2012 01:32, Soichi Ishida <lists@ruby-forum.com> wrote: > When it comes to Facebook, the create method in services_controller.rb > > omniauth['provider'] ? provider = omniauth['provider'] : > end > services.build(:provider => provider, :uid => uid, :token => > token, :secret => secret) > end > > > at line 18 where I indicated by putting "###HERE!" in the code 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. Colin
on 2012-12-03 04:30
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. soichi
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.