Topic: Cookie Expiration Trouble

I have been looking all over the web and reading documentation, and I can't seem to find this very simple answer. How do I set the expiration of the non-perminant cookie? The permanent cookie works well (20 year expiration), but the not permanent cookie also has a 20 year expiration! What gives? and how are you supposed to set the expiration when there is no hash of arguments to pass :expiration into? In a perfect world the non-permanent cookie would expire upon browser closing, but how to set that?

def sign_in(user, permanent)
    if permanent
      cookies.permanent.signed[:remember_token]=[user.id, user.salt]
    else
      cookies.signed[:remember_token]=[user.id, user.salt]
    end
    
    self.current_user = user
end

Re: Cookie Expiration Trouble

Does anybody have any ideas on how to get a remember me checkbox to work in rails 3? How do I set a cookie to expire on close? or how do you set the expiration for a signed cookie?

Re: Cookie Expiration Trouble

For posterity's sake, there were two issues with my previous code. First a checkbox value comes through rails as either 0 or 1 so

if permanent
...

becomes

if permanent == 1
...

And cookies.signed will take an a hash, but the value needs to be explicit.

cookies.signed[:key]={:value => [user.id, user.salt], :option => ...}

So, the final code was...

def sign_in(user, permanent)
  if permanent == 1
    cookies.permanent.signed[:remember_token]=[user.id, user.salt]
  else
  cookies.signed[:remember_token]={:value => [user.id, user.salt], :expires => 3.hours.from_now}
  end
end