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

发布于 2024-10-02 19:24:42 字数 593 浏览 3 评论 0原文

我是一名学生,目前就读信息技术课程,并获得了一个项目,要求我和我的团队使用 Rails 3 + Ruby 1.9.2 创建动态表单生成器。这个动态表单生成器的一个关键功能是让用户导出表单的结果。我在使用 Ruby 1.9+ API 中定义的 CSV 类实现 CSV 功能方面并没有取得太大成功。我在 form_results 控制器中定义了一个“导出”函数,目前我只是尝试写入 CSV 文件。我的导出函数如下所示:

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

在视图中,我通过以下方式链接到该函数:

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

我觉得如果我能让 CSV 类的实现正常工作,我将能够完成其余的逻辑,而无需任何认真的处理问题。任何指导、意见或帮助将不胜感激。

谢谢,

马兹·M。

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. 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. I define an "export" function in the form_results controller and am currently just trying to write to a CSV file. 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. Any guidance, input or help will be greatly appreciated.

Thanks,

Maz M.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人海汹涌 2024-10-09 19:24:42

您对 :method 参数的使用不正确。这应该用于指定 http 动词。文档在这里:
http://api.rubyonrails.org/classes/ActionView /Helpers/UrlHelper.html#method-i-link_to

我建议在查看动态表单的操作中使用 respond_to 块,并使用 format.csv< /代码>。这允许您使用相同的操作,但仅通过调用操作 URL 并将 .csv 附加到 URL 来以不同的格式呈现结果

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

然后在您的表单模型中,您可以创建一个 to_csv def 它将呈现 csv 并将其返回为一个字符串。你真的不应该在你的控制器中放置任何这样的逻辑。控制器用于创建实例变量(创建逻辑应在模型中完成)并转发到正确的视图。该模型应该包含大部分逻辑。谷歌“瘦控制器,胖模型”以获取更多信息。

def to_csv
  csv = some_logic_here_to_create_csv_string
  csv
end

你的 link_to 调用可能看起来像这样(只是在我的脑海中写下这个......我不记得这是否是正确的语法):

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

Your use of the :method param is incorrect. This should be used to specify http verbs. Documentation here:
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. 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

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. 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 'Download CSV', form_form_results_path(@form, :format=>:csv) %>
债姬 2024-10-09 19:24:42

您应该参考链接。本 Railscast 剧集介绍了如何以 CSV 格式或 XLS 格式导出数据。您还可以参考链接。

You should refer this link. This Railscast episode explains to export data in either CSV format or XLS format. You can also refer this link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文