Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem Changing Cookie Value in Rails 3

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
like image 236
Dex Avatar asked Nov 14 '22 02:11

Dex


1 Answers

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

like image 146
Dex Avatar answered Dec 31 '22 19:12

Dex