質問

I've got a rails app where I'm using a combination of authlogic and cancan.

when I check for current_user in the app, I always get undefined variable or method 'current_user'

I can get the current_user to display from my UserSessions controller if I use

def create
   @user_session = UserSession.new(params[:user_session])

   if @user_session.save
       return render :json => current_user
   end
end

however, if in my Home controller, I try

def index
   return render :json => current_user
end

I get the undefined error.

In my Application Helper, I have

class ApplicationController < ActionController::Base
  protect_from_forgery

  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

so it seems to me the session is being created, but rails is unable to find it again. Can anybody shed some light on why that is?

I've gone through the Ryan Bates tutorial, and a few others, but it all seems the same to me.

役に立ちましたか?

解決

Not really an answer to the question, but I ended up stripping out authlogic and moved to devise. It was much faster to make the change than all the time I spent trying to debug the issue in authlogic.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top