class Controller < ApplicationController
def index
respond_to do |format|
format.csv do
headers["X-Accel-Buffering"] = "no"
headers["Cache-Control"] = "no-cache"
headers["Content-Type"] = "text/csv; charset=utf-8"
headers["Content-Disposition"] =
%(attachment; filename="#{csv_filename}")
headers["Last-Modified"] = Time.zone.now.ctime.to_s
self.response_body = build_csv_enumerator
end
end
end
private
def build_csv_enumerator
Enumerator.new do |yielder|
yielder << headers_to_csv
Account.find_each do |account|
yielder << account.to_csv_row
end
end
end
end
This allows your users to download files more efficiently. Especially for huge files.