如何使用axlsx下载索引页的search结果?

首先,如果这个问题太微不足道,我真的很抱歉。 我是新的铁轨,不知道我在哪里做错了。

我有一个名为成本核算模型,在它的索引页面我有一个search表单。 我正在尝试使用“axlsx”gem来下载search结果,但我总是得到所有的行。 我也使用'will_paginate'gem。

这是我的代码。

//costings_controller.rb def index @costings = Costing.search(params[:search] , params[:page]) respond_to do |format| format.html # index.html.erb format.json { render json: @costings } format.xlsx { send_data @costings.to_xlsx.to_stream.read, :filename => 'costings.xlsx', :type => "application/vnd.openxmlformates-officedocument.spreadsheetml.sheet" } end end // index.html.erb <%= link_to 'Download Costings', url_for(:format=>"xlsx") %> 

请帮我这里。 非常感谢。

以下是我为axlsx gem演示所做的代码。 浏览它并在控制器中实现您的要求。 这是这个演示的输出。

 //costings_controller.rb def download @costings = Costing.search(params[:search] , params[:page]) respond_to do |format| format.html # index.html.erb format.json { render json: @costings } format.xlsx { send_data @costings.to_xlsx.to_stream.read, :filename => 'costings.xlsx', :type => "application/vnd.openxmlformates-officedocument.spreadsheetml.sheet" } end end // index.html.erb <%= form_for :costing, url: download_costing_index_path do %> ... <% end %> //routes resources :costing do collection do post :download, :defaults => { :format => 'xlsx' } end end