I'm trying to change a cookie for the users location in a before filter but am having issues:
The cookie gets set to 1 correctly if it doesn't exist, but will not save permanently and reverts back to 1 for any subsequent requests.
def remember_location(loc = nil)
cookies.permanent[:location] = 1 if cookies[:location].nil?
loc = Location.find(loc).try(:id) rescue nil
unless loc.nil?
# cookies.delete :location # => this doesn't work either
cookies.permanent[:location] = loc
end
cookies[:location]
end
Here was the problem. The locations I was entering afterwards were giving a Rails error since the DB wasn't fully populated. The cookie won't actually get saved unless the entire endpoint request completes successfully.
After looking at the source code for ActionDispatch::Cookies, that certainly seems to be the case: http://api.rubyonrails.org/classes/ActionDispatch/Cookies.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With