21

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?

3
  • 3
    Looks pretty comprehensive and handles data relations, I'd say stick with comma Commented Nov 29, 2010 at 8:44
  • 1
    Comma does not work for me in rails3. I found github.com/econsultancy/csv_builder and it works well. Commented Nov 29, 2010 at 10:48
  • 1
    Can confirm that comma is not working in Rails 3. Commented Dec 15, 2010 at 14:44

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Works great with rails 3.x too.
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.
2

Checkout this Stack Overflow answer for using CSV in Ruby 1.9.x (which, as Fletch noted, includes FasterCSV but with slightly different syntax).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.