Steps to download as CSV
Ruby has csv handling libraries so just call
require ‘csv’- in config/application.erb
in controller
respond_to do |format|
format.html format.csv{ render text: @products.to_csv} format.xls{ render text: @products.to_csv(col_sep: "\t")} end
this converts the html to both csv and xls
in model
def self.to_csv(options={}) CSV.generate(options) do |csv| csv << column_names all.each do |product| csv << product.attributes.values_at(*column_names) end end end —————–
and in your index page just at browser address bar just add .csv or .xls the file gets downloaded if you want to add link in page then
<%= link_to “CSV”, products_path(format: “csv”) %> <%= link_to “xls”, products_path(format: “xls”) %>