-
-
Save lazypower/4962534 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def export_inverts | |
require 'fastercsv' | |
inverts = Invert.all | |
filename = params[:action] + ".csv" | |
#this is required if you want this to work with IE | |
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}\"" | |
headers["Content-Transfer-Encoding"] = "binary" | |
end | |
csv_string = FasterCSV.generate do |csv| | |
csv << ["Genus","Species","Common Name","Pet Name","Gender"] | |
inverts.each do |i| | |
csv << [i.scientific_name,i.scientific_name,i.common_name,i.pet_name,i.gender] | |
end | |
end | |
render :text => csv_string | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment