简体   繁体   English

Rails 3从部分导出CSV?

[英]Rails 3 export csv from partial?

I followed RBate's Railscast on the subject , but I have a caviat. 我关注了RBate的Railscast ,但是我有一个鱼子酱。

The view starts with a form to define the table's variables. 该视图以定义表变量的形式开始。 On clicking submit, the form posts back to the same view and re-renders the partial (which is just the HTML table). 单击提交后,表单将发回到同一视图,然后重新呈现部分视图(即HTML表)。 It's not AJAX, it's all form params. 它不是AJAX,而是形式参数。

RBates uses a single variable and calls .to_csv on it. RBates使用单个变量并在其上调用.to_csv。 I have a complex table with many variables defined by the params and so don't know how to call .to_csv on the entire HTML table. 我有一个复杂的表,其中包含由params定义的许多变量,因此不知道如何在整个HTML表上调用.to_csv。

I need to be able to export just the results in the partial. 我需要能够只导出部分结果。

Thanks in advance for the ideas. 预先感谢您的想法。

&&&&&&&& AS REQUESTED, SAMPLE CODE: &&&&&&&& &&&&&&&&要求,示例代码:&&&&&&&

So, the full code is probably more confusing than helpful, but here's a slimmed down version: 因此,完整的代码可能比帮助更令人困惑,但这是一个精简的版本:

stats/reporter.html.erb: (stat_reporter_path matches this URI) stats / reporter.html.erb: (stat_reporter_path与此URI匹配)

<%= form_tag( :stat_reporter, method: :post ) do %>
    form is in here
    <%= submit_tag "Get my report" %>
<% end %>

<div>
    <%= render 'stat_select' %>
</div>

stats/_stat_select: stats / _stat_select:

<table>
  <thead>
    <tr>...column heads...</tr>
  </thead>
  <tbody>
    <% @counties.each do |c| %>
      <td><%= c.name %></td>
        <% c.programs.each do |p| %>
          <td><%= p.name %></td>
            <% p.events.each do |e| %>
              <td><%= e.name %></td>
...and so on...
</table>

Here's a screenshot 这是截图 屏幕截图

Looking back on this post, I would: 回顾这篇文章,我会:

  1. Create a link to a new view and pas all the variables to in the URL. 创建一个指向新视图的链接,并将所有变量粘贴到URL中。
  2. That view would use all the variables to create an HTML output that could be used to create the to_csv method 该视图将使用所有变量来创建可用于创建to_csv方法的HTML输出。
  3. Once the csv file was created, the controller would render the original view. 创建csv文件后,控制器将呈现原始视图。

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

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