Skip to content

Instantly share code, notes, and snippets.

@amxn
Created February 28, 2014 16:39
Show Gist options
  • Save amxn/9274332 to your computer and use it in GitHub Desktop.
Save amxn/9274332 to your computer and use it in GitHub Desktop.
Generate a CSV from a collection action.
collection_action :download_report, method: :get do
users = User.where('created_at >= ?', Date.today - 1.month)
csv = CSV.generate( encoding: 'Windows-1251' ) do |csv|
# add headers
csv << [ #Some header ]
# add data
users.each do |user|
csv << [ user.created_at ]
end
end
# send file to user
send_data csv.encode('Windows-1251'), type: 'text/csv; charset=windows-1251; header=present', disposition: "attachment; filename=report.csv"
end
action_item only: :index do
link_to "csv report", download_report_admin_#{models}_path, method: :get
end
index download_links: false do
# off standard download link
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment