Learn how to export records into CSV files using Ruby on Rails. Here are the steps.
Steps:
- Add a controller and make sure you handle the csv request.
- Add a route to point to your controller
- Add a model with the class method to_csv
Lets code it!
Controller
# users_controller.rb
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html
format.csv { send_data @users.to_csv, filename: "users-#{Date.today}.csv" }
end
end
Routes
# routes.rb
...
resources :users, only: :index
...
Model
# user.rb
class User < ActiveRecord::Base
def self.to_csv
attributes = %w{id email name}
CSV.generate(headers: true) do |csv|
csv << attributes
all.find_each do |user|
csv << attributes.map{ |attr| user.send(attr) }
end
end
end
def name
"#{first_name} #{last_name}"
end
end
Core custom methods:
to_csv
will map out the "id, email, name" values of your model collection and then generate a CSV string.
Rails core methods for this feature:
#send_data
Sends the given binary data to the browser. This method is similar to render :text => data, but also allows you to specify whether the browser should display the response as a file attachment (i.e. in a download dialog) or as inline data. You may also set the content type, the apparent file name, and other things. Source
Nitpicking if I may, a missing last or first name won't be noticed in HTML but it will be noticed in CSV when a human reads it directly or imported in Excel/LibreCalc.