如何从Rails 3中的方法传回一个对象?

时间:2011-04-03 18:56:31

标签: ruby-on-rails ruby callback debugging models

我的Omniauth回调中有以下内容:

@user = User.find_for_linked_in_oauth(env["omniauth.auth"], current_user)

我希望有任何一种方法来获取用户对象。她是用户方法。现在,我正在测试无法找到现有用户的情况,因为它尚未创建:

   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

目前,使用调试器,我得到'1'作为p @user的值。

我希望将新创建的用户对象传回,这样我就可以在保存之前让当前用户添加其他信息。

为什么我没有将新创建的用户作为@user?

的值

2 个答案:

答案 0 :(得分:2)

尝试更改地点userdebugger,以便它返回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

答案 1 :(得分:0)

可能是调试器语句搞乱了。尝试交换语句,如:

debugger
user

在你的功能结束时。