Skip to content

Instantly share code, notes, and snippets.

@yorzi
Last active September 25, 2015 19:38
Show Gist options
  • Save yorzi/973786 to your computer and use it in GitHub Desktop.
Save yorzi/973786 to your computer and use it in GitHub Desktop.
render csv sample action
def export_to_csv
@users = User.find(:all)
csv_string = CSV.generate do |csv|
# header row
csv << ["id", "first_name", "last_name"]
# data rows
@users.each do |user|
csv << [user.id, user.first_name, user.last_name]
end
end
# send it to the browsah
send_data csv_string,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=users.csv"
end
def render_csv
compile_data
out = [["Korean Name", "English Name", "User ID", "Cohort Name", "Teacher", "Attended Count", "Absent Coun", "Total", "Attended Rate"].join(",")]
@results.each{|r| out << r.join(",")}
out = out.join("\n\r")
filename = resource.course.name + params[:starting] + "-" + params[:ending] + ".csv"
headers["Content-Type"] = 'text/csv; charset=UTF-8'
headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
render :text => out
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment