Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export data to CSV in rails [closed]

Tags:

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?

like image 682
boblin Avatar asked Nov 29 '10 08:11

boblin


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 
like image 102
hade Avatar answered Sep 22 '22 20:09

hade


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

like image 43
douglasr Avatar answered Sep 21 '22 20:09

douglasr