Reputation: 2720
I am developing an application in rails 3
How do i access session in lib classes in Rails.
Thanks
Upvotes: 4
Views: 4177
Reputation: 103
in Rails, ApplicationController
has a method named session
, use session[:user_id]
to fetch the session value. Thus, if you want use session in lib, you need define a method in lib classes which access session as parameter.
lib/your_class.rb
class YourClass
def set_session(session_)
@mysession = session_
end
def session
return @mysession
end
end
The simple way is that, you can use module instead of lib class, and include the module in your Controller. Then you can use session[:user_id]
in the lib module any way.
Upvotes: 3