Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4无代码复制的索引视图CSV生成_Ruby On Rails_Csv_Ruby On Rails 4_Dry_Code Duplication - Fatal编程技术网

Ruby on rails Rails 4无代码复制的索引视图CSV生成

Ruby on rails Rails 4无代码复制的索引视图CSV生成,ruby-on-rails,csv,ruby-on-rails-4,dry,code-duplication,Ruby On Rails,Csv,Ruby On Rails 4,Dry,Code Duplication,如何将我的命名空间/resource/index.html.erb内容导出到CSV文件中(该内容在索引页面上的每一行使用分部),而不复制CSV视图文件中的代码(或以任何方式输出CSV) 控制器如下所示: class Namespaced::ResourceController < AncestorController def index @rows = current_object.rows.some_scope.page(params[:page]) respond

如何将我的命名空间/resource/index.html.erb内容导出到CSV文件中(该内容在索引页面上的每一行使用分部),而不复制CSV视图文件中的代码(或以任何方式输出CSV)

控制器如下所示:

class Namespaced::ResourceController < AncestorController
  def index
    @rows = current_object.rows.some_scope.page(params[:page])

    respond_to do |format|
      format.html
      format.csv do
        headers['Content-Disposition'] = "attachment; filename=\"filename.csv\""
        headers['Content-Type'] ||= 'text/csv'
     end
    end
  end
end
类名称空间::ResourceController
“索引”视图仅列出表标题:

<div class="container-fluid">
      <div class="row">
        <div class="col-lg-12">
          <table class="table">
            <thead>
              <tr>
                <th>
                  <strong>Order ID</strong>
                </th>
                <th>
                  <strong>Order Date</strong>
                </th>
                <th>
                  <strong>Description</strong>
                </th>
                <th>
                  <strong>Price</strong>
                </th>
                <th>
                  <strong>Shipping</strong>
                </th>
                <th>
                  <strong>Tax</strong>
                </th>
                <th>
                  <strong>Total</strong>
                </th>
                <th>
                  <strong>Status</strong>
                </th>
              </tr>
            </thead>
            <tbody>
              <%= render partial: "row", collection: @rows %>
            </tbody>
          </table>
        </div>
      </div>
      <div class="row">
        <div class="col-lg-12">
          Download: <%= link_to 'CSV', namespaced_resource_path(format: :csv) %>
        </div>
      </div>
    </div>

订单ID
订单日期
说明
价格
配送
税收
总计
状态
下载:
部分HTML视图如下所示:

<tr>
  <td>
    <%= row.order_id %>
  </td>
  <td>
    <%= row.row_date %>
  </td>
  <td>
    <%= row.description %>
  </td>
  <td>
    <%= row.price %>
  </td>
  <td>
    <%= row.shipping %>
  </td>
  <td>
    <%= row.tax %>
  </td>
  <td>
    <%= row.calculate_total %>
  </td>
  <td>
    <%= row.status %>
  </td>
</tr>

…CSV视图如下所示:

<%- headers = ['Order ID', 'Order Date', 'Description', 'Price', 'Shipping', 'Tax', 'Total', 'Status'] -%>
<%= CSV.generate_line headers %>
<%- @rows.each do |entry| -%>
  <%= CSV.generate_line([
    row.order_id,
    row.row_date,
    row.description,
    row.price,
    row.shipping,
    row.tax,
    row.calculate_total,
    row.status
  ]) -%>
<%- end -%>

请注意,部分HTML视图和CSV视图使用相同的属性/回调。其中一些是相对复杂的decorator/helper回调,将来可能会更改/省略/添加更多。在这两个位置都添加了标题(它们不仅仅是数据模型的属性)。所以基本上,这个部分视图和CSV视图显示代码重复

虽然我可以想出一些技术来填充列标题并使用相同的回调函数,但我不确定这是否是常规方法。我有一种预感,有一种简单的方法可以实现这一点,它构建在框架中


什么是最接近Rails的干燥方法?我怎么能有一个既适用于HTML索引视图又适用于CSV视图的局部视图呢?

现在我想起来了,可能没有一种“Railsy”方法可以做到这一点——Rails生成器在为控制器操作创建HTML和JSON视图时会复制代码。如果有办法在它们之间共享相同的过程,我想生成器会自动完成。