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?
-
3Looks pretty comprehensive and handles data relations, I'd say stick with commaocodo– ocodo2010-11-29 08:44:44 +00:00Commented Nov 29, 2010 at 8:44
-
1Comma does not work for me in rails3. I found github.com/econsultancy/csv_builder and it works well.boblin– boblin2010-11-29 10:48:02 +00:00Commented Nov 29, 2010 at 10:48
-
1Can confirm that comma is not working in Rails 3.Fletch– Fletch2010-12-15 14:44:38 +00:00Commented Dec 15, 2010 at 14:44
Add a comment
|
2 Answers
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
3 Comments
Fletch
FasterCSV 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.
David Radcliffe
Works great with rails 3.x too.
hade
Thanks @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.
Checkout this Stack Overflow answer for using CSV in Ruby 1.9.x (which, as Fletch noted, includes FasterCSV but with slightly different syntax).