# CookieStore and setting an expiration

**URL:** <https://www.ruby-forum.com/t/cookiestore-and-setting-an-expiration/166991>\
**Category:** Rails\
**Created:** [May 1, 2009, 7:08pm UTC](https://www.ruby-forum.com/t/cookiestore-and-setting-an-expiration/166991 "2009-05-01T19:08:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![etienne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9fc348/32.png) [@etienne](https://www.ruby-forum.com/u/etienne)\
**Post date:** [May 1, 2009, 7:08pm UTC](https://www.ruby-forum.com/t/cookiestore-and-setting-an-expiration/166991/1 "2009-05-01T19:08:24Z")

</div>

hi everyone,

i can’t seem to find any help online for this. I’m using  
CookieStore. What I can’t seem to figure out how to do is set the  
expiration for the cookie set by CookieStore. Right now it leaves  
that field blank, meaning that the cookie expires when the browser is  
closed. The context is for a login, where the user has the option to  
check a “leave me logged in” option that would expire the cookie 2  
weeks or 3 weeks later.

thanks,  
etienne

---

<div class="post-metadata">

**Author:** ![etienne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9fc348/32.png) [@etienne](https://www.ruby-forum.com/u/etienne)\
**Post date:** [May 1, 2009, 7:18pm UTC](https://www.ruby-forum.com/t/cookiestore-and-setting-an-expiration/166991/2 "2009-05-01T19:18:20Z")

</div>

cookies[:key] = {:value =\> value, :expires =\> 3.weeks.from\_now}

---

<div class="post-metadata">

**Author:** ![etienne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9fc348/32.png) [@etienne](https://www.ruby-forum.com/u/etienne)\
**Post date:** [May 3, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/cookiestore-and-setting-an-expiration/166991/3 "2009-05-03T18:12:54Z")

</div>

That overwrites the cookie for the session. By the time they attempt  
to login, a session has already begun for them. CookieStore is doing  
the management behind the scenes. I want to amend the cookie to have  
an expiration date which, it seems, it does not have by default.  
etienne

---

<div class="post-metadata">

**Author:** ![etienne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9fc348/32.png) [@etienne](https://www.ruby-forum.com/u/etienne)\
**Post date:** [May 5, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/cookiestore-and-setting-an-expiration/166991/4 "2009-05-05T15:23:44Z")

</div>

etienne [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> That overwrites the cookie for the session. By the time they attempt  
> to login, a session has already begun for them. CookieStore is doing  
> the management behind the scenes. I want to amend the cookie to have  
> an expiration date which, it seems, it does not have by default.  
> etienne

Try this in your config/environment.rb

config.action\_controller.session = {  
:session\_key =\> ‘\_yourapp’,  
:secret =\> ‘yoursecret’,  
:expire\_after =\> 600 # seconds  
}

It works for me.

Jarl
