Ruby-on-rails 从 Rails 生成 CSV 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2472694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate CSV file from rails
提问by Elliot
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):
然后根据 read me,我把它扔到我的模型中(根据我的需要定制):
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 异常错误:
undefined method `comma' for
未定义的方法`逗号'
So then I googled that, and I read that I should put require 'comma' in my model.
然后我用谷歌搜索,我读到我应该在我的模型中加上 require 'comma'。
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. I then refreshed the page and landed on this error:
所以在这一点上,我决定显然不能找到逗号文件。所以我将文件从逗号的供应商 gem 文件夹,从逗号的 lib 文件夹复制到 rails lib 文件夹。然后我刷新了页面并出现了这个错误:
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:inconst_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:in
load_missing_constant' /Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:inconst_missing' /Users/elliot/.gem/ruby/1.8/gems/activesupport-2.3 .5/lib/active_support/dependencies.rb:92:in`const_missing'
Other notes, I have already installed FasterCSV
其他注意事项,我已经安装了 FasterCSV
Hope thats enough info :)
希望这是足够的信息:)
采纳答案by theIV
回答by Toby Hede
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)
回答by Jason Lewis
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.
这很丑陋,但很有效。
回答by paul
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 模型配合使用。

