Topic: Where to put this helper routine?.

I have some helper routines for setting session data.

def setsessiondata(user)
  session[:user_id] = user.id
  ...
end

def updaterecentitems(type, id)
  session[:r0type] = type
  session[:r0id] = id
  ...
end

Im having a hard time figuring out where to put it so I can use it from many different places.

The first place I want to use it is when a user logs in. But, I will also want to use it from different controllers so that I can keep track of 'recent items'.

If I put it in my LoginController, it works at login, but I dont know if Ill be able to use it elsewhere.
Ive tried putting it in the login_helper - but it doesnt 'see' it from the controller. I think the helpers are only accessable from the views, right? Similarly, Ive tried putting it in to the application_helper, but the same thing happens.
Finally, Ive tried putting it in to various models, but they dont have access to the 'session' hash.

Should I just put it in one of the controllers? Will other controllers be able to access it? (Like LoginController.updaterecentitems or something like that?)

Many thanks,

Re: Where to put this helper routine?.

I suggest putting the methods inside a helper module: helpers/session_helper.rb.

module SessionHelper
  def setsessiondata(user)
    #...
  end
end

If you want the methods accessible in the controller, you can simply include the module. If you want it accessible in the view, use the helper method. You can do this in ApplicationController to make methods accessible in all views/controllers, or just do it for the specific controllers.

# place either of these, or both, in a controller
include SessionHelper # includes in controller
helper SessionHelper # includes in views

If the methods are just setting the session, then the view shouldn't need access to this as setting a session should be handled by the controller.

Railscasts - Free Ruby on Rails Screencasts

Re: Where to put this helper routine?.

ryanb rules....