[Rails] Facebook omniauth nil object
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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home