Forum: Ruby on Rails Application.rb / Application_Helper.rb / DRY help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
648805ae18e92b7c71e46f3caa23ddc8?d=identicon&s=25 Ed C. (Guest)
on 2005-12-19 07:03
(Received via mailing list)
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
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 Marcel Molina Jr. (Guest)
on 2005-12-19 07:06
(Received via mailing list)
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
This topic is locked and can not be replied to.