Mar 22, 2010

IE Error : File Download on Rails Form Post

If you are using Rails, you many notice that some form posts from IE (6/7) result in IE asking you where you want to save a file, even if your response is supposed to be a redirect. It took me a while to figure out what was happening here so thought I’d share it and save someone else the trouble.

It seems that IE doesn’t always send the correct accepts headers for a post. Rails (v2.3.5 at least) if it can’t find a matching format in a responds_to block, will just render the first response.

So, make sure you always order your responds_to blocks so that html is first.

def action_name
  # my action code
  respond_to do |format|
    # Always make sure HTML is first otherwise
    # you'll send a js response to IE!
    format.html { redirect_to '/' }
    format.js { }

Otherwise, when you submit a form, Rails will render the js result, which IE sees as a file download.