Skip to content

Instantly share code, notes, and snippets.

@manojmj92
Last active January 23, 2019 10:32
Show Gist options
  • Save manojmj92/63ed977a7b3ee19470fbfa5d6994acb0 to your computer and use it in GitHub Desktop.
Save manojmj92/63ed977a7b3ee19470fbfa5d6994acb0 to your computer and use it in GitHub Desktop.
# In the controller#action
respond_to do |format|
format.csv { render_streaming_csv(generate_csv(column_names, records)) }
end
def render_streaming_csv(csv_enumerator)
# Delete this header so that Rack knows to stream the content.
headers.delete("Content-Length")
# Do not cache results from this action.
headers["Cache-Control"] = "no-cache"
# Let the browser know that this file is a CSV.
headers['Content-Type'] = 'text/csv'
# Do not buffer the result when using proxy servers.
headers['X-Accel-Buffering'] = 'no'
# Set the filename
headers['Content-Disposition'] = "attachment; filename=\"report.csv\""
# Set the response body as the enumerator
self.response_body = csv_enumerator
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment