Module: RenderedCsv::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/rendered_csv/controller.rb

Instance Method Summary collapse

Instance Method Details

#render_csv(filename = nil, template = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rendered_csv/controller.rb', line 7

def render_csv(filename = nil, template = nil)
  filename ||= params[:action]
  filename += '.csv'

  if request.env['HTTP_USER_AGENT'] =~ /msie/i
    headers['Pragma'] = 'public'
    headers["Content-Type"] = "text/plain"
    headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
    headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
    headers['Expires'] = "0"
  else
    headers["Content-Type"] ||= 'text/csv'
    headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
  end

  if template.nil?
    render :layout => false
  else
    render template, :layout => false
  end
end