authlogic current_user是nil

时间:2010-08-24 19:11:51

标签: ruby-on-rails authlogic

我的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对象的attempts_record存在且指向 到正确的用户,但是在current_user方法中 redirect_to edit_user_path(current_user), 总是返回零。

这里出了什么问题?

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 

3 个答案:

答案 0 :(得分: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。

我需要回家,今晚我会回来看看。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

尝试将此用于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]