I’m using Rails 4.2.7. I want to define a filter subject to certain conditions. So I have
class ApplicationController < ActionController::Base
before_filter :store_last_page_visited unless logged_in?
…
private
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
def logged_in?
!current_user.nil?
end
def store_last_page_visited
session[:last_page_visited] = request.url
end
helper_method :current_user, :logged_in?
However, when I visit a page in my application, I get the error
undefined method `logged_in?' for ApplicationController:Class Did you mean? logger
What do I need to do differently to get my filter executed when I specify?
before_filter
is executed in the context of the class.
#logged_in?
is defined in the context of the class instance.
Try this:
def store_last_page_visited
return if logged_in?
session[:last_page_visited] = request.url
end