简体   繁体   English

从Rails生成CSV文件

[英]Generate CSV file from rails

I've been reading similar questions, but many of the answers are outdated or not clear enough for me. 我一直在阅读类似的问题,但是许多答案对我而言已经过时或不够清晰。

I'd like to be able to just do something like (in a controller action): 我希望能够做类似的事情(在控制器动作中):

respond_to do |format|
  format.html
  format.csv
end

I know I'd then need a view such as action.csv.erb 我知道我需要一个诸如action.csv.erb之类的视图


So my questions are: 所以我的问题是:

1) What do I need to configure in rails to allow this to happen in general. 1)我需要在rails中进行配置以使这种情况大体上发生。

2) How should I setup the CSV view to display some basic fields from a model? 2)如何设置CSV视图以显示模型中的一些基本字段?

UPDATE: 更新:

So I've tried to go the route of comma, I installed and vendored the gem. 因此,我尝试走逗号的路线,安装并销售了gem。

Then according to the read me, I threw this into my model (customized to my needs): 然后根据阅读的书,我把它扔进了我的模型中(根据我的需要定制):

comma do

user_id 'User'
created_at 'Date'
name 'Name'
end

I then threw this in the control for the index action (according to the readme): 然后,将其放入索引操作的控件中(根据自述文件):

  format.csv { render :csv => MyModel.limited(50) }

Then when accessing the index (not in CSV format) I receive the following ActionController Exception error: 然后,当访问索引(不是CSV格式)时,出现以下ActionController Exception错误:

undefined method `comma' for 未定义的方法'逗号'

So then I googled that, and I read that I should put require 'comma' in my model. 因此,我在Google上进行了搜索,并阅读了我应该在模型中添加“逗号”的信息。

After doing that, I refreshed (my local index page), and the error changed to: 完成之后,我刷新了(我的本地索引页面),错误更改为:

no such file to load -- comma 没有要加载的此类文件-逗号

So at this point I decided it must not be finding the comma files obviously. 因此,在这一点上,我决定显然不能找到逗号文件。 So I copied the files from the vendored gem folder of comma, from comma's lib folder, to the rails lib folder. 因此,我将文件从逗号的供应商gem文件夹(从逗号的lib文件夹)复制到rails lib文件夹。 I then refreshed the page and landed on this error: 然后,我刷新页面并发现此错误:

uninitialized constant Error 未初始化的常量错误

Then I pretty much gave up. 然后我几乎放弃了。

The errors from the trace were: 跟踪中的错误是:

/Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:443:in load_missing_constant' /Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in const_missing' /Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:92:in `const_missing' /Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:443:在load_missing_constant' /Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in const_missing中/Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:92 load_missing_constant' /Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in const_missing中

Other notes, I have already installed FasterCSV 其他说明,我已经安装了FasterCSV

Hope thats enough info :) 希望那是足够的信息:)

I suggest taking a look at comma . 我建议看一下逗号 It works very well and allows you to handle stuff at the model level, as opposed to the view level. 它工作得很好,并允许您在模型级别(而不是视图级别)处理内容。

Have a look at FasterCSV. 看看FasterCSV。

csv_string = FasterCSV.generate do |csv|

  cols = ["column one", "column two", "column three"]

  csv << cols

  @entries.each do |entry|                
    csv << [entry.column_one, entry.column_two, entry.column_three ]
  end

  filename = "data-#{Time.now.to_date.to_s}.csv"    

end

send_data(csv_string, :type => 'text/csv; charset=utf-8; header=present', :filename => filename)  

This is terrible, but the CSV library (in 1.9, == FasterCSV) won't play nice with meta_where, so I did it this way: 这很糟糕,但是CSV库(在1.9中,== FasterCSV)在meta_where上无法很好地发挥作用,所以我这样做是这样的:

@customers.collect {|c| lines.push ["#{c.lastname}","#{c.firstname}","#{c.id}","#{c.type}"}
lines = lines.collect {|line| line.join(',')}
csv_string = lines.join("\n")  
respond_to do |format|
  format.html
  format.csv { send_data(csv_string, :filename => "#{@plan.name.camelize}.csv", :type => "text/csv") }
end

It's ugly, but effective. 难看,但是有效。

Take a look at CSV Shaper. 看看CSV Shaper。

https://github.com/paulspringett/csv_shaper https://github.com/paulspringett/csv_shaper

It has a nice DSL and works really well with Rails models. 它具有良好的DSL,并且可以与Rails模型一起很好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM