Application.rb / Application_Helper.rb / DRY help


#1

Hi all,

I have defined the following method in both application.rb and
application_helper.rb:

def still_logged_in?
!session[:user_id].nil?
end

This method is called in a before_filter in my application.rb, as well
as in my layout/application.rhtml and view/user/login.rhtml (for
showing a login / logout button, etc.)

I had to put it both places, otherwise I would get an ‘undefined method’
error.

Is there a better approach I should take to DRY this duplicate logic up?

Any suggestions appreciated!

Thanks,
Best Regards,
EJC


#2

On Mon, Dec 19, 2005 at 01:01:54AM -0500, Ed C. wrote:

Hi all,

I have defined the following method in both application.rb and
application_helper.rb:

def still_logged_in?
!session[:user_id].nil?
end
helper_method :still_logged_in?

This method is called in a before_filter in my application.rb, as well
as in my layout/application.rhtml and view/user/login.rhtml (for
showing a login / logout button, etc.)

I had to put it both places, otherwise I would get an ‘undefined method’ error.

Is there a better approach I should take to DRY this duplicate logic up?

helper_method will make still_logged_in? available to your views as
well.

marcel