-Define a timeout method -Even though the code itself is not wrong, the session is judged as nil, the method is not processed well, and an error occurs.
sessions_controller.rb
def create
# ...
session[:last_access_time] = Time.current
# ...
end
application_controller.rb
TIMEOUT = 5.minutes
def time_out
if session[:last_access_time] > TIMEOUT.ago
session[:last_access_time] = Time.current
else
session.delete(:user_id)
flash[:danger] = "Timed out."
redirect_to :login
end
end
When I build the above method and then try to log in, an error occurs.
NoMethodError (undefined method `>=' for nil:NilClass):
# session[:last_access_time]Is judged to be nil.
I solved it by deleting the cookie on the browser side. Probably because I defined the method while logged in You may have been angry at "there is no session [: last_access_time]".
Recommended Posts