I am reading this excellent tutorial right now and got the same task about 30 minutes ago. Jeremy Roman answered exactly this question. Although he did not give a phased answer, what he mentioned allowed me to find out the answer.
Now, to be more specific ... Given that Jeremy mentioned that sessions expired automatically after closing the browser, unless the “Expires” attribute is specified for the corresponding cookie, the solution is related to the following code fragment:
app/helpers/session_helper.rb:
def sign_in(user) cookies.permanent.signed[:remember_token] = [user.id, user.salt] self.current_user = user end
According to the tutorial itself , the permanent method above sets the cookie attribute 'Expires' to 20.years.from_now . Thus, this means that your session will persist for 20 years if you do not exit explicitly or delete your cookie.
So, to end a session when the browser is closed, you just need to create your cookie without any expiration date, which boils down to a simple pass to permanent :
app/helpers/session_helper.rb:
def sign_in(user) cookies.signed[:remember_token] = [user.id, user.salt] self.current_user = user end
Although there may be other valid solutions that are not yet obvious to me (after all, I just started to learn Rails), this one looks very simple - you don’t even need to write anything - just delete permanent and you go there! :)
source share