如何在 Rails 中为我的整个索引视图使用 send_data 方法,而不仅仅是第一页?

How do I use the send_data method in Rails for my entire index view and not just the first page?

我希望能够以 csv 格式从我的订单 table 下载我的索引数据。我已经能够使用以下代码实现此功能:

orders controller:

class OrdersController < ApplicationController
    before_action :find_order, only: [:edit, :destroy, :update, :show]

    def index
        @orders = Order.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 20)
        # to allow csv and xls formats to be downloaded
        respond_to do |format|
            format.html 
            format.csv { send_data @orders.to_csv }
            format.xls { send_data @orders.to_csv(col_sep: "\t") } # tab separate to work with Excel
        end
    end

订购型号:

def self.to_csv(options = {}) 
        CSV.generate(options) do |csv|
            csv << column_names
            all.each do |order|
                csv << order.attributes.values_at(*column_names)
            end
        end
    end

但是,由于我有分页,这只能让我下载订单 table 中的前 20 个条目。我以为下面的代码可以修复它,但它并没有改变任何东西:

def index
        @orders = Order.all
        # to allow csv and xls formats to be downloaded
        respond_to do |format|
            format.html { @orders.order("created_at DESC").paginate(:page => params[:page], :per_page => 20) }
            format.csv { send_data @orders.to_csv }
            format.xls { send_data @orders.to_csv(col_sep: "\t") } # tab separate to work with Excel
        end
    end

app/views/orders/index.html.erb:43:in `_app_views_orders_index_html_erb___1955221700531553779_70231785858000'
Started GET "/" for 127.0.0.1 at 2018-11-28 01:19:56 +0100
Processing by OrdersController#index as HTML
  Rendering orders/index.html.erb within layouts/application
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 3], ["LIMIT", 1]]
  Order Load (0.9ms)  SELECT  "orders".* FROM "orders" ORDER BY created_at DESC LIMIT ? OFFSET ?  [["LIMIT", 10], ["OFFSET", 0]]
   (0.3ms)  SELECT COUNT(*) FROM "orders"
  Rendered orders/index.html.erb within layouts/application (44.6ms)
Completed 200 OK in 216ms (Views: 203.2ms | ActiveRecord: 6.4ms)


Started GET "/orders.csv" for 127.0.0.1 at 2018-11-28 01:20:03 +0100
Processing by OrdersController#index as CSV
  Order Load (0.6ms)  SELECT  "orders".* FROM "orders" ORDER BY created_at DESC LIMIT ? OFFSET ?  [["LIMIT", 10], ["OFFSET", 0]]
  Rendering text template
  Rendered text template (0.0ms)
Sent data  (4.7ms)
Completed 200 OK in 15ms (Views: 4.5ms | ActiveRecord: 0.6ms)

关于如何保持分页但能够下载订单中的所有条目的任何想法 table?

我认为这可能就像将 .paginate 调用移动到 format.html { ... } 块中一样简单

class OrdersController < ApplicationController
    before_action :find_order, only: [:edit, :destroy, :update, :show]

    def index
        orders = Order.all.order("created_at DESC")

        # to allow csv and xls formats to be downloaded
        respond_to do |format|
            format.html do 
                @orders = orders.paginate(:page => params[:page], :per_page => 20)
            end
            format.csv { send_data orders.to_csv }
            format.xls { send_data orders.to_csv(col_sep: "\t") } # tab separate to work with Excel
        end
    end

    ...
end

可以 必须在 format.html 块中添加 render :index。我目前无法测试此代码。