I am exporting a custom array to a csv file. Everything is exporting correctly, however it is not adding a comma delimiter between each instance. What am I missing in my code to add this separation?
data_services_controller
def export_all
@list = DataService.all
respond_to do |format|
format.csv { render text: @list.to_csv }
end
end
data_service.rb
def self.to_csv
CSV.generate do |csv|
csv << ["Data Product", "Requestor"]
all.each do |dp|
csv << [dp.name ,dp.requestor]
end
end
end
list.html.erb
<%= link_to "Export to CSV", export_all_data_services_path(:format => :csv), :class => "btn btn-primary" %>
@list.to_csv is calling the to_csv method on the relation returned by DataServive.all and not the class method you defined.
calls your to_csv class method. You won't need the @list instance variable.