Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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")) %>
%= link_to "Download Excel", yours_controller_path(params.merge(format: "xls")) %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top