Ransack Search Results - to_xls?

三世轮回 提交于 2019-12-21 05:45:48

问题


I have a ransack search form which is working wonderfully, I would like to add an export for the user to send the contents of the result set to an XLS file.

I have implemented the to_xls sucessfully as well, however it is giving me back the fullest possible scope of the object I am searching, and not the filtered results that are shown in the view.

def index

  @search = Expense.search(params[:q])
  @expense_list = @search.result.sort_by(&:expense_date) 

    respond_to do |format|
      format.html
      format.xml { render :xml => @expense_list }
      format.xls { send_data @expense_list.to_xls, :filename => '123.xls'}
    end

end

Does it have something to do with how ransack uses the GET method? Any help would be great.

Thanks!


回答1:


I know this is such a hack, after spending many hours of not getting it, I used it anyway.

<a href="/expenses.xls?<%= request.fullpath.split("?")[1]  %>">make xls</a>

so basically it takes the searchpath after the ?, then adds it to your model.xls output path and then it works. I hate it myself, there must be a better way, but deadlines.

There was a good link here.




回答2:


Ronin gave a simple solution to this related question, but with CSV instead of XLS . In my case, using Ronin's answer, I just rewrote the link to work with XLS as shown below

<%= link_to "Download Excel", reports_path(params.merge(format: "xls")) %>



回答3:


%= link_to "Download Excel", yours_controller_path(params.merge(format: "xls")) %>


来源:https://stackoverflow.com/questions/14127222/ransack-search-results-to-xls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!