Export to CSV in Rails
Exporting data to CSV from a Rails app is as simple as adding a respond_to block to the controller action and setting the proper response headers.
As an example, let’s consider a users#index action which lists the users for admin. Now if you want to allow the admin to download the list as CSV, this is how it can be done:
Require Ruby’s built in CSV library in ApplicationController.
In users#index, add a respond_to block with HTML and CSV formats. In the format.csv
block, set the ‘Content-Type’ and the ‘Content-Disposition’ headers. (The latter lets you set the name of the downloaded file.)
Create an erb file at app/views/users/index.csv.erb with the CSV data. Let’s only include name and email fields for this example.
Now you can link to your CSV download using the normal link helpers with the format option like this:
And that’s it. It just works.