Ruby on Rails Friday, November 30, 2012

On 30 November 2012 01:32, Soichi Ishida <lists@ruby-forum.com> wrote:
> 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

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

--
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.

No comments:

Post a Comment