Peel me a Grape :: We make things work

We Make Things Work :: Blog

Came up against a small gotcha with the behaviour of respond_to.

My controller action code looked like:

def new
  respond_to do |wants|
    wants.js {render :partial => 'new'}
    wants.html
  end
end

which works fine in Firefox – but does not work in IE.

Ordering of the options in the respond_to block is important.

My controller now does this instead
def new
  respond_to do |wants|
    wants.html
    wants.js {render :partial => 'new'}
  end
end

The reason why it works in Firefox and not in IE is because of the Accept headers the browsers send.

Firefox sends:
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
IE sends:
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*

IE doesn’t explicitly accept (x)html mime types – both js and html will match the wildcard mimetype. It works in firefox because it (sensibly) gives precedence to application/xhtml+xml and text/html. It is also worth noting that Firefox gives high precedence to xml – another reason why ordering will be important.

blog comments powered by Disqus