Prototype, Ajax, Firefox, Caching

January 5, 2008 Pivotal Labs

By default, Ajax.Updater will send a POST request to whatever URL is specified. That’s fine, and it prevents certain browser caching issues, but it won’t play well with Rails’ resource routes. Rails will map POST requests to the create controller action, which is not always desirable.

For example, let’s say we have an Ajax.Updater that refreshes a list of posts (GET /posts). Unfortunately, the request that is created by default will be different (POST /posts). You can force the GET method, like so

remote_function(:update => "posts_container",
:url => posts_path, :method => :get))

One problem remains, however. If the URL for the remote_function and the URL for the full (html) version of the resource are the same, as they probably ought to be, and if the Ajax call is a GET request, Firefox will cache the data returned by the remote_function call as the most recent version of the page /posts. When a user navigates away from /posts, and subsequently decides to return to /posts by hitting the back button, the remote_function data will be displayed (as opposed to the full html version of the page). That’s not very user-friendly.

To avoid this, one will need to set the appropriate header:

headers["Cache-Control"] = "no-store"

Interestingly enough, IE7 appears to deal with this situation correctly.

About the Author

Biography

More Content by Pivotal Labs
Previous
ruby-debug in 30 seconds (we don't need no stinkin' GUI!)
ruby-debug in 30 seconds (we don't need no stinkin' GUI!)

Many people (including me) have complained about the lack of a good GUI debugger for Ruby. Now that som...

Next
The Power of Versions (Monkey Patches Targeted with Friggin Laser Beams!)
The Power of Versions (Monkey Patches Targeted with Friggin Laser Beams!)

We all love to Monkey Patch Rails and other Ruby apps. However, we sometimes want to target these patches ...

Enter curious. Exit smarter.

Register Now