简体   繁体   English

如何在Ruby 1.9.2 + Rails3中将表数据导出为CSV文件?

[英]How to export table data as CSV file in Ruby 1.9.2 + Rails3?

I am a student that currently enrolled in a Information Technology program and have been given a project that requires my team and I to create a dynamic form builder using Rails 3 + Ruby 1.9.2. 我是一名学生,目前正在参加信息技术计划,并且获得了一个项目,该项目需要我和我的团队使用Rails 3 + Ruby 1.9.2创建动态表单构建器。 A key feature of this dynamic form builder will be for users to export the results of their form. 该动态表单构建器的关键功能是让用户导出其表单结果。 I haven't had much success implementing the CSV feature using the CSV class defined in the Ruby 1.9+ API. 使用Ruby 1.9+ API中定义的CSV类实现CSV功能并没有取得太大的成功。 I define an "export" function in the form_results controller and am currently just trying to write to a CSV file. 我在form_results控制器中定义了一个“导出”功能,当前仅尝试写入CSV文件。 My export function looks like this: 我的导出函数如下所示:

def export
 CSV.open("/public/results/results.csv", "wb") do |csv|
   csv << ["sample", "data"]
 end
end

And in the view, I link to the function by using: 在视图中,我使用以下方法链接到该函数:

<%= link_to 'Download CSV', form_form_results_path(@form), :method => :export %>

I feel that if I can get the implementation of the CSV class working properly, I will be able to finish off the rest of logic without any serious issues. 我认为,如果我可以使CSV类的实现正常工作,那么我将可以完成其余逻辑,而不会遇到任何严重问题。 Any guidance, input or help will be greatly appreciated. 任何指导,意见或帮助将不胜感激。

Thanks, 谢谢,

Maz M. 马兹M.

Your use of the :method param is incorrect. 您对:method参数的使用不正确。 This should be used to specify http verbs. 这应该用于指定http动词。 Documentation here: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to 此处的文档: http : //api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

I would suggest using the respond_to block in the action where they are viewing the dynamic form, and use format.csv . 我建议在他们查看动态表单的操作中使用respond_to块,并使用format.csv This allows you to use the same action, but render the results in a different format by merely calling the action URL with .csv appended to the URL 这使您可以使用相同的操作,但只需调用操作URL,并将URL附加.csv即可以不同的格式呈现结果。

respond_to do |format|
  format.html
  format.csv { render :text => @dynamic_form.to_csv} #this will return txt in browser
  #format.csv { render :csv => @dynamic_form.to_csv} #with mime type (should prompt browser download)
end

Then in your form model you can create a to_csv def which will render the csv and return it as a string. 然后,您可以在表单模型中创建to_csv def,该def将呈现csv并将其作为字符串返回。 You really should not put any logic like this in your controller. 您实际上不应该在控制器中放入任何类似的逻辑。 Controllers are meant for creating instance variables (where creation logic should be done in models) and forwarding to the proper views. 控制器用于创建实例变量(应在模型中完成创建逻辑)并转发到适当的视图。 The model should contain the bulk of your logic. 该模型应包含您的大部分逻辑。 google "skinny controllers, fat models" for more info on that. 谷歌“瘦控制器,胖模型”的更多信息。

def to_csv
  csv = some_logic_here_to_create_csv_string
  csv
end

Your link_to call would probably look like this (just writing this off the top of my head.. I cant remember if this is the correct syntax): 您的link_to调用可能看起来像这样(只是将其写在我的头上。我不记得这是否是正确的语法):

<%= link_to 'Download CSV', form_form_results_path(@form, :format=>:csv) %>

You should refer this link. 您应该参考链接。 This Railscast episode explains to export data in either CSV format or XLS format. 这个Railscast插曲介绍了以CSV格式或XLS格式导出数据。 You can also refer this link. 您也可以参考链接。

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

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