How to access a session in lib classes in Rails

I am developing an application in rails 3

How do I access a session in lib classes in Rails.

thanks

+4
source share
1 answer

in Rails, ApplicationController has a method called session , use session[:user_id] to retrieve the session value. Thus, if you want to use the session in lib, you need to define a method in the lib classes that access the session as a parameter.

Library / your _class.rb

 class YourClass def set_session(session_) @mysession = session_ end def session return @mysession end end 

A simple way is that you can use the module instead of the lib class and include the module in your controller. Then you can use session[:user_id] in the lib module in any way.

+3
source

All Articles