How do I pass back an object from a method in Rails 3?
I have the following in my Omniauth callback:
@user = User.find_for_linked_in_oauth(env["omniauth.auth"], current_user)
I expect either way to get a user object. Her is the User method. Right now, I am testing the case where it cannot find an existing User because it hasn't been created yet:
def self.find_for_linked_in_oauth(omniauth_hash, signed_in_resource=nil)
debugger
#omniauth_hash is a hash passed in from env["omniauth_hash"] by callback controller
linkedin_uid = omniauth_hash['uid']
debugger
if user = User.find_by_linkedin_uid(linkedin_uid)
debugger
user
else # Create an user with a stub password.
#redirect to a page to ask for an email address and display information
#User.create!(:email => "token@email.com", :linkedin_uid => linkedin_uid, :password => Devise.friendly_token[0,20])
user = User.new
user.first_name = omniauth_hash['user_info']['first_name']
user.last_name = omniauth_hash['user_info']['last_name']
user.linkedin_uid = linkedin_uid
user
debugger
end
Currently, with debugger, I get back '1' as the 开发者_如何学Govalue for p @user.
I want the newly created user object to be passed back so I can then ask the current user to add additional information before saving it.
Why am I not getting the newly created user as the value of @user?
try to change places user
and debugger
so it will return user
19 def self.find_for_linked_in_oauth(omniauth_hash, signed_in_resource=nil)
20 debugger
21 #omniauth_hash is a hash passed in from env["omniauth_hash"] by callback controller
22 linkedin_uid = omniauth_hash['uid']
23 debugger
24 if user = User.find_by_linkedin_uid(linkedin_uid)
25 debugger
26 user
27 else # Create an user with a stub password.
28 #redirect to a page to ask for an email address and display information
29 #User.create!(:email => "token@email.com", :linkedin_uid => linkedin_uid, :password => Devise.friendly_token[0,20])
30 user = User.new
31 user.first_name = omniauth_hash['user_info']['first_name']
32 user.last_name = omniauth_hash['user_info']['last_name']
33 user.linkedin_uid = linkedin_uid
34
36 debugger
35 user
37 end
It could be that the debugger statement is messing things. Try exchanging statements like :
debugger
user
in the end of your function.
精彩评论