为什么只导出第一页的结果到csv?

时间:2022-08-21 00:18:36

I am trying to get ALL the filtered results when exporting to CSV.

当导出到CSV时,我试图获取所有过滤的结果。

My to_csv is working fine, and I suspect something to do with my controller.

我的to_csv工作正常,我怀疑它与我的控制器有关。

Search around with kaminari, ransack, but seems like even exporting to csv is rare using ransack.
Any help is much appreciated.

搜索与kaminari, ransack,但似乎即使出口到csv也是罕见的使用ransack。非常感谢您的帮助。

controller.rb

controller.rb

@search = Order.includes(:user, :order_items).ransack(params[:q])
@orders = @search.result.order(created_at: :desc).page(params[:page])

respond_to do  
  |format|
  format.html
  format.csv { send_data @orders.to_csv, filename: "orders-#{DateTime.now.strftime("%d%m%Y%H%M")}.csv" }
end

view.html.haml

view.html.haml

= link_to 'Download as CSV', a_o_path(request.params.merge(format: 'csv')), { class: 'btn btn-primary', style: 'float: right;' }

1 个解决方案

#1


2  

You can do this with ransack and kaminari, but you'll need to update your controller just a bit. Here's what you have so far:

你可以用ransack和kaminari来做这个,但是你需要稍微更新你的控制器。以下是迄今为止你所拥有的:

format.csv { send_data @orders.to_csv, filename: "orders-#{DateTime.now.strftime("%d%m%Y%H%M")}.csv" }

When your controller gets here, @orders has been filtered by ransack, but it has also been paginated by kaminari. Since you're responding with html in some cases and csv in others, you'll want to do something slightly different when you respond with csv.

当您的控制器到达这里时,@orders已被ransack过滤,但它也已被kaminari分页。由于您在某些情况下使用html响应,而在其他情况下使用csv响应,所以在使用csv响应时,您需要做一些稍微不同的事情。

Here's what I would try:

以下是我想尝试的:

@search = Order.includes(:user, :order_items).ransack(params[:q])
@orders = @search.result.order(created_at: :desc)

respond_to do |format|
  format.html { @orders = @orders.page(params[:page]) }
  format.csv { send_data @orders.to_csv, filename: "orders-#{DateTime.now.strftime("%d%m%Y%H%M")}.csv" }
end

Basically, you only paginate the query when you respond with html. When csv is requested, all of the orders will still be there.

基本上,您只在使用html响应时对查询进行分页。当请求csv时,所有的命令仍然在那里。

#1


2  

You can do this with ransack and kaminari, but you'll need to update your controller just a bit. Here's what you have so far:

你可以用ransack和kaminari来做这个,但是你需要稍微更新你的控制器。以下是迄今为止你所拥有的:

format.csv { send_data @orders.to_csv, filename: "orders-#{DateTime.now.strftime("%d%m%Y%H%M")}.csv" }

When your controller gets here, @orders has been filtered by ransack, but it has also been paginated by kaminari. Since you're responding with html in some cases and csv in others, you'll want to do something slightly different when you respond with csv.

当您的控制器到达这里时,@orders已被ransack过滤,但它也已被kaminari分页。由于您在某些情况下使用html响应,而在其他情况下使用csv响应,所以在使用csv响应时,您需要做一些稍微不同的事情。

Here's what I would try:

以下是我想尝试的:

@search = Order.includes(:user, :order_items).ransack(params[:q])
@orders = @search.result.order(created_at: :desc)

respond_to do |format|
  format.html { @orders = @orders.page(params[:page]) }
  format.csv { send_data @orders.to_csv, filename: "orders-#{DateTime.now.strftime("%d%m%Y%H%M")}.csv" }
end

Basically, you only paginate the query when you respond with html. When csv is requested, all of the orders will still be there.

基本上,您只在使用html响应时对查询进行分页。当请求csv时,所有的命令仍然在那里。