Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm reading this great tutorial right now and got to the same assignment some 30 minutes ago. <strong>Jeremy Roman</strong> answered exactly the question in this thread. Though he didn't provide a step-by-step answer, the thing that he mentioned, allowed me to figure out the answer.</p> <p>Now to be more specific... Considering what <strong>Jeremy</strong> mentioned about sessions being expired automatically after the browser is closed, unless there is a "Expires" attribute specified for the corresponding cookie, the solution is related to the following chunk of code:</p> <p><code>app/helpers/session_helper.rb:</code></p> <pre class="lang-rb prettyprint-override"><code>def sign_in(user) cookies.permanent.signed[:remember_token] = [user.id, user.salt] self.current_user = user end </code></pre> <p>As per the <a href="http://ruby.railstutorial.org/chapters/sign-in-sign-out#code%3asign_in_function" rel="nofollow">tutorial itself</a>, the <code>permanent</code> method above is setting the 'Expires' cookie attribute to <code>20.years.from_now</code>. So this means that your session will persist for 20 years, unless you log out explicitly or delete your cookie.</p> <p>So in order to expire your session when the browser is closed, you just need to create your cookie without any expiration date, which boils down to simply omitting <code>permanent</code>:</p> <p><code>app/helpers/session_helper.rb:</code></p> <pre class="lang-rb prettyprint-override"><code>def sign_in(user) cookies.signed[:remember_token] = [user.id, user.salt] self.current_user = user end </code></pre> <p>Though there may be other valid solutions, which are not obvious for me yet (after all I just started learning Rails), this one looks <em>very</em> simple - you don't even need to write anything - just delete <code>permanent</code> and there you go! :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload