I try to add sessions to my rails app and I have class SessionManager
which is responsible for handling session related tasks
class SessionManager
include SessionsHelper
def log_in(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= User.find_by_id(session[:user_id])
end
def logged_in?
return false if current_user.nil?
true
end
end
as you can see I include SessionsHelper
in order to use session
method to put some data into session, but when I try to use this code I get back:
NameError: undefined local variable or method `session' for <SessionManager:0xa790cfc>
What do I do wrong ?
Thanks in advance.
I am not sure what your SessionsHelper module looks like, but you may want to look at using self.included. This is sometimes known as a "mixin".
I believe structuring your module similar to this should help you:
module SessionsHelper
def self.included(base)
base.class_eval do
def session
# ...
end
end
end
end
More info on included: http://ruby-doc.org/core-2.2.0/Module.html#method-i-included
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.