Take the 2-minute tour ×

Rails cookies,

i need to set start date and expire date using cookie,

for eg., once the added item reaches third day, the item should expire.

share|improve this question
add comment

2 Answers

up vote 123 down vote accepted

Cookies are read and written through ActionController#cookies (ActionDispatch#cookies in Rails 3 or greater).


Examples for writing:

  # Sets a simple session cookie.
  cookies[:user_name] = "david"

  # Sets a cookie that expires in 1 hour.

Examples for reading:

  cookies[:user_name] # => "david"
  cookies.size        # => 2

Example for deleting:

  cookies.delete :user_name


 cookies[:key] = {
   :value => 'a yummy cookie',
   :expires => 1.year.from_now,
   :domain => 'domain.com'


The option symbols for setting cookies are:

* :value - The cookie’s value or list of values (as an array).
* :domain - The domain for which this cookie applies.
* :expires - The time at which this cookie expires, as a Time object.
share|improve this answer
This is all in the docs: api.rubyonrails.org/classes/ActionDispatch/Cookies.html –  Guilherme Garnier Mar 7 '13 at 13:07
@Senthil, If you are getting something from another article (in this case 'as is' from rails docs), Please give a reference to the origin. –  sameera207 Mar 31 at 1:43
add comment

your question might be related to this question: How to I dynamically set the expiry time for a cookie-based session in Rails

one of the comments points to Deprecating SlideSessions:


:expire_after => 60.minutes


request.session_options = request.session_options.dup
request.session_options[:expire_after]= 5.minutes


I hope that helps. :)

share|improve this answer
add comment

Your Answer

