Search code examples
ruby-on-railsransack

Ransack Search Results - to_xls?


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!


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.