0

I'm hitting this bug in my application, where ajax in mobile safari stops working after receiving a response with Content-Disposition:attachment

I want to keep sending disposition:attachment to desktop browsers and to non-iOS mobile devices, while switching to disposition:inline on iOS. I need to do this in a few different controller actions.

Is there an elegant way of doing this beyond putting these types of blocks all over the place?

if request.env['HTTP_USER_AGENT'] =~ /iPad/
    disposition = :inline
else 
    disposition = :attachment
end
Community
  • 1
  • 1
spike
  • 9,794
  • 9
  • 54
  • 85

1 Answers1

2

Just create a before_filter in application controller that will set it once and for all!

class ApplicationController < ActionController::Base
  before_filter :set_content_disposition

  def set_content_disposition
    if request.env['HTTP_USER_AGENT'] =~ /iPad/
      response.headers['Content-Disposition'] = 'inline'
    elsif params[:format].in?(['pdf', 'other_format', 'other_format2'])
      response.headers['Content-Disposition'] = 'attachment'
    end
  end
  #Rest of application controller code ...
end
Anthony Alberto
  • 10,325
  • 3
  • 34
  • 38
  • Hmm, I can't set :attachment for *all* requests, because then the browser will try to download every page. Is there a way to set a header for all responses of a given format, so that every time I do `respond_to ... format.pdf` it sets the header? – spike Aug 02 '12 at 17:01
  • Seems like a reasonable approach. – spike Aug 02 '12 at 17:08