使用 fastcsv 和 CSV::Writer (Ruby on Rails) 导出到 csv
我想做什么:将数据导出到 csv。
我有一个允许用户选择格式的表单(从下拉菜单中)。因此,根据格式的选择,使用 ajax 调用显示输出。对于 html 工作正常,但当我选择 csv 格式时,我在屏幕上没有看到任何弹出窗口(要求保存或打开文件),也没有直接下载任何文件。
我尝试使用 Fastercsv (但问题是我没有看到任何弹出窗口询问我是否要保存或打开文件)和 CSV::Writer ,我在控制台上收到此错误消息。
NoMethodError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.bytesize):
actionpack (2.3.4) lib/action_controller/streaming.rb:142:in `send_data'
使用 Fastercsv 的代码:
def export_to_csv
csv_string = FasterCSV.generate(:col_sep => ",") do |csv|
members = ["Versions / Project Members"]
members_selected.each {|member| members << Stat.member_name(member)}
Stat.project_members(project).each {|user| members << user.name}
csv << ["some text", "text 2", "text 3"]
end
return csv_string
end
这就是我发送数据的方式:
send_data(export_to_csv,:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment", :filename => "filename.csv")
我在 Firebug 控制台中看到响应为“一些文本,文本 2,文本 3”,但没有弹出窗口询问我是否要保存或打开文件。
这就是我正在使用 CSV::Writer: 所做的事情,
def export_to_csv
report = StringIO.new
CSV::Writer.generate(report, ',') do |csv|
csv << ['c1', 'c2']
end
end
并将其称为:
send_data(export_to_csv,:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment", :filename => "filename.csv")
这是控制台上抛出的错误:
NoMethodError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.bytesize):
actionpack (2.3.4) lib/action_controller/streaming.rb:142:in `send_data'
What am I trying to do: export data to csv.
I have a form which allows user to select the format (from a drop down menu). So based on the selection of the format the ouput is displayed using a ajax call. Works fine for html but when I select the format as csv I don't see any pop up on the screen (asking to save or open the file) and neither any file gets downloaded directly.
I tried using Fastercsv (but the problem is that I don't see any pop up asking me whether I want to save or open the file) and CSV::Writer where I get this error message on the console.
NoMethodError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.bytesize):
actionpack (2.3.4) lib/action_controller/streaming.rb:142:in `send_data'
Code using Fastercsv:
def export_to_csv
csv_string = FasterCSV.generate(:col_sep => ",") do |csv|
members = ["Versions / Project Members"]
members_selected.each {|member| members << Stat.member_name(member)}
Stat.project_members(project).each {|user| members << user.name}
csv << ["some text", "text 2", "text 3"]
end
return csv_string
end
and this is how I am sending the data:
send_data(export_to_csv,:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment", :filename => "filename.csv")
I see the response as "some text, text 2, text 3" in the firebug console but no pop up asking whether I want to save or open the file.
This is what I am doing using CSV::Writer:
def export_to_csv
report = StringIO.new
CSV::Writer.generate(report, ',') do |csv|
csv << ['c1', 'c2']
end
end
and call it as:
send_data(export_to_csv,:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment", :filename => "filename.csv")
This is the error which is thrown on the console:
NoMethodError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.bytesize):
actionpack (2.3.4) lib/action_controller/streaming.rb:142:in `send_data'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
send_data 试图引用超出范围的对象。检查你的结束语“结束”
send_data is trying to reference an object that is out of scope. Check your closing 'end' statement