I need export data as CSV in rails appl. I found this plugin: https://github.com/crafterm/comma. Do you know about some better solution?
Asked
Active
Viewed 2.2k times
21
-
3Looks pretty comprehensive and handles data relations, I'd say stick with **comma** – ocodo Nov 29 '10 at 08:44
-
1Comma does not work for me in rails3. I found https://github.com/econsultancy/csv_builder and it works well. – boblin Nov 29 '10 at 10:48
-
1Can confirm that comma is not working in Rails 3. – Fletch Dec 15 '10 at 14:44
2 Answers
38
If using Ruby 1.9.x, then use CSV rather than FasterCSV and stick with the default delimiters.
Controller:
respond_to do |format|
...
format.csv { render :layout => false }
end
show.csv.erb:
<%= this_is_your_view_helper_method.html_safe %>
controller_helper.rb:
require 'csv'
def this_is_your_view_helper_method
CSV.generate do |csv|
Product.find(:all).each do |product|
csv << ... add stuff here ...
end
end
end

pguardiario
- 53,827
- 19
- 119
- 159

hade
- 1,715
- 1
- 16
- 24
-
10FasterCSV actually became the standard CSV library in Ruby 1.9, so no need to download it, it's already there if you're on Ruby 1.9. – Fletch Dec 15 '10 at 14:48
-
1
-
1Thanks @Fletch for the note! This answer has been downvoted twice and I have no idea why. If you downvote, please let me know why you're doing so. – hade Mar 05 '12 at 07:29