Last active
September 25, 2015 19:38
-
-
Save yorzi/973786 to your computer and use it in GitHub Desktop.
render csv sample action
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_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 |
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 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