ruby-on-rails
  • ruby-on-rails-3
  • 2012-05-02 46 views 0 likes 
    0

    以我rails应用程序我执行以下步骤:Rails的物体分配混乱

    用户需要登录 - >用户创建的授权 - >的用户被创建

    该应用程序似乎被绊倒该过程的最后一步。

    我成功地创建一个授权:

    Authorization Load (0.9ms) SELECT "authorizations".* FROM "authorizations" WHERE "authorizations"."provider" = 'facebook' AND "authorizations"."uid" = '2259711' LIMIT 1 
    Authorization attributes hash: {"id"=>1, "uid"=>"2259711", "provider"=>"facebook", "user_id"=>1, "created_at"=>Wed, 02 May 2012 06:06:13 UTC +00:00, "updated_at"=>Wed, 02 May 2012 06:06:13 UTC +00:00} 
    

    但随后扼流圈用户创建步骤:

    User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    Completed 500 Internal Server Error in 26ms 
    
    RuntimeError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id): 
        app/controllers/sessions_controller.rb:21:in `create' 
    
    会话控制器的

    21行是这样的:session[:user_id] = @auth.user.id

    但是这使得没有意义,因为我已经清楚地初始化了一个用户对象的正确ID ...

    管辖此步骤中的代码如下:

    活动#创建控制器

    def create 
        # create the auth hash here 
        auth_hash = request.env['omniauth.auth'] 
    
        if session[:user_id] 
         # Means our user is signed in. Add the authorization to the user 
         User.find(session[:user_id]).add_provider(auth_hash) 
    
         redirect_back_or User.find(session[:user_id]) 
    #  render :text => "You can now login using #{auth_hash["provider"].capitalize} too!" 
        else 
         # Log him in or sign him up 
         @auth = Authorization.find_or_create(auth_hash) 
    
         # Create the session 
         logger.debug "Person attributes hash: #{@auth.attributes.inspect}" 
         session[:user_id] = @auth.user.id 
    #  render :text => "Welcome #{auth.user.name}!" 
    
         sign_in @auth.user 
         redirect_back_or @auth.user 
        end  
        end 
    

    授权find_or_create模型方法

    def self.find_or_create(auth_hash) 
         unless auth = find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"]) 
          user = User.create :name => auth_hash["info"]["name"], :email => auth_hash["info"]["email"] 
          logger.debug "Within Authorization find_or_create: Person attributes hash: #{user.attributes.inspect}" 
          auth = create :user => user, :provider => auth_hash["provider"], :uid => auth_hash["uid"] 
         end 
         auth 
        end 
    

    UPDATE

    当我试试这个:logger.debug "User attributes hash: #{@auth.user.attributes.inspect}" 在上面的会话控制器我得到这个错误:

    NoMethodError (undefined method `attributes' for nil:NilClass): 
        app/controllers/sessions_controller.rb:21:in `create' 
    

    这意味着用户对象为零。这不应该是这种情况,因为我在授权find_or_create方法内呼叫user.create ...

    +0

    @ auth的任何属性设置(提供者或uid)? –

    +0

    是的,他们都设置。 –

    回答

    0

    我不认为find_by_provider_and_uid设置用户属性,所以如果成功,你需要考虑到这一点。

    auth = find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"]) 
    
    if auth.nil? # i don't know if nil is the right condition to check for, my active record fu is not up to date. 
        # do your unless code 
    else 
        #need to fetch and set user property 
    end 
    
    auth 
    
    +0

    你是对的,我遇到了一个边缘案例,我在数据库中有一个悬而未决的授权(用户对象被删除)。应该添加逻辑来处理这种情况。谢谢! –

    0

    这可能是用户验证失败并且实际上并未被创建。将User.create更改为User.create!,如果验证失败,您将立即看到它,因为它会引发异常。

    相关问题