Reputation: 1771
I know how to change the expiration time when saving a cookie, what I would like to know is how to change the default value. I have a lot of sentences that set cookies scattered around my code and I have to change all of them to expire after 30 days, so I would prefer to just change something that sets the default expiration time to 30 days so that it affects all cookies everywhere. Thanks in advance!
Update Tried using:
# config/initializers/session_store.rb
MyApp::Application.config.session_store :cookie_store, key: '_your_app_session', expire_after: 30.days
But still doesn't works... I think I know what the problem is. When I set the cookie expiration at the moment I save it, it then appears under "Response Cookies" on both Firefox and Chrome (that's when it works) But the rest of the cookies appear under "Request Cookies" on both explorers. The thing is that I don't know how the Request/Response thing is handled by rails.
This is how I save my cookies, which doesn't work.
cookies[:region] = params[:region]
And this is how I made it work, but would prefer the other way to save time and make it easier to edit in the future.
cookies[:region] = { value: params[:region], expires: 30.days.from_now }
Update 2: Created a new rails app to show the problem isolated: https://github.com/jafuentest/newapp
Upvotes: 2
Views: 2243
Reputation: 2175
You can save it in session_store:
# config/initializers/session_store.rb
MyApp::Application.config.session_store :cookie_store, key: '_your_app_session', expire_after: 30.days
Upvotes: 1