开发者

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.pe开发者_如何学Pythonrmanent[: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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜