【问题标题】:authlogic current_user is nilauthlogic current_user 为 nil
【发布时间】:2012-09-04 21:42:25
【问题描述】:

我的 application_controller 中有标准的 current_user 方法。

   def current_user_session
        return @current_user_session if defined?
(@current_user_session)
        @current_user_session = UserSession.find
    end

    def current_user
        return @current_user if defined?(@current_user)
        @current_user = current_user_session &&
current_user_session.record
    end

在我的 UserSessionController 创建方法中,我检查了 注册完成? (只需检查列的值,即 工作正常),然后将用户重定向到用户编辑页面,如果 注册未完成。当我调试这个时,我可以看到 @user_session 对象的尝试记录存在并且它指向 到正确的用户,但 current_user 方法在 重定向到编辑用户路径(当前用户), 总是返回 nil。

这里出了什么问题?

def create
        @user_session = UserSession.new(params[:user_session])
        # uses a block to prevent double render error...
        # because oauth and openid use redirects
        @user_session.save do |result|
            if result
                flash[:notice] = "Login successful!"
                if @user_session.new_registration?
                    logger.info "Session controller : new
registration"
                    logger.info "Complete -
#{@user_session.registration_complete?}"
                    flash[:notice] = "Please review profile"
                    redirect_to edit_user_path(current_user)
                else
                    if @user_session.registration_complete?
                        logger.info "Session Controller - registration
complete"
                    else
                        flash[:notice] = "Please complete profile"
                        logger.info "Session Controller - registration
not complete"
                        redirect_to edit_user_path(current_user)
#current_user nil here
                    end
                end
                redirect_to current_user ? profile_url(current_user) :
login_url
            else
                if @user_session.errors.on(:user)
                    # if we set error on the base object, likely it's
because we didn't find a user
                    render :action => :confirm
                else
                    render :action => :new
                end
            end
        end
    end 

【问题讨论】:

标签: ruby-on-rails authlogic


【解决方案1】:

我已经看到了这一点,并在我的一个应用程序中使用了相同的身份验证系统。你没有提到它,这就是为什么我向你提出这个建议,如果你有,那么我需要看到更多你的 application_controller,所以无论如何尝试在方法之前添加它。

helper_method :current_user

我只是有一个无聊的时刻,尝试这样做而不是将块传递给 save 方法。

if @user_session.save
  # rest of your logic here
end

“如果结果”可能不会像您期望的那样。

否则,请检查此 @user_session.record 而不是 current_user,如果它仍然为空,则您的问题不是 current_user。

我要回家了,今晚晚些时候再回来看看。

【讨论】:

  • 感谢您的回复。这是我的应用程序控制器。我已经有了那个辅助方法。 pastie.org/1113118
  • 好的,我觉得没问题,你能告诉我 @user_session.new_registration 下的 current_user 吗?验证是非空的?我试图找出任何问题但我不能,我在想@user_session.registration_complete?可能会改变@user_session 里面的内容。否则将 current_user 存储在另一个变量中,例如 @user 然后在重定向中使用它来查看它是否有效。
  • 实际上 current_user 是否在该创建方法中的任何位置工作?
  • 其实,不是。我已经用 authlogic-connect 的 session.rb 文件更新了这个馅饼,这是我用于认证的 gem。它包含注册完成方法。
  • 谢谢雨果。 authlogic-connect 需要为 oauth 身份验证传递一个块,因为它是一个两步过程。
【解决方案2】:

我在 authlogic 和 rails 3 上遇到了类似的问题,我发现的问题是 在我的生产服务器中启用的 apache http 身份验证, 这是解决该问题的解决方案,请查看Rails + Authlogic @current_user problem - "Show" method no longer works

【讨论】:

    【解决方案3】:

    尝试在rails2中使用这个:

    class ApplicationController < ActionController::Base
      helper :all # include all helpers, all the time
      protect_from_forgery # See ActionController::RequestForgeryProtection for details
    
      #Authlogic
      filter_parameter_logging :password
    
      helper_method :current_user
    
      private
    
      def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = UserSession.find
      end
    
      def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.record
      end
    end
    

    这个到 rails3:

    class ApplicationController < ActionController::Base
      helper :all # include all helpers, all the time
      protect_from_forgery # See ActionController::RequestForgeryProtection for details
    
      helper_method :current_user
    
      private
    
      def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = UserSession.find
      end
    
      def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.record
      end
    end
    

    /config/application.rb

    # Configure sensitive parameters which will be filtered from the log file.  
    config.filter_parameters += [:password]  
    

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多