将数据导出到导轨中的CSV [英] Export data to CSV in rails

查看:123
本文介绍了将数据导出到导轨中的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在rails appl中将数据导出为CSV。我找到此插件: https://github.com/crafterm/comma

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?

推荐答案

如果使用Ruby 1.9.x,那么使用CSV而不是FasterCSV,默认分隔符。

If using Ruby 1.9.x, then use CSV rather than FasterCSV and stick with the default delimiters.

控制器:

respond_to do |format|
  ...           
  format.csv { render :layout => false }
end

show.csv.erb:

show.csv.erb:

<%= this_is_your_view_helper_method.html_safe %>

controller_helper.rb:

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

这篇关于将数据导出到导轨中的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆