Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.1rubygems

Why doesn't the extension for the filename get saved for my XLS downloads?


I'm trying to export to an XLS file. The file is being saved but without an extension. When I download the file I need to manually add ".xls" to specify it's an Excel file.

Here is my code from my view:

  <%= link_to("Export","http://localhost:3000/policy_management/policy/generate_print_ejecutive/generate_print_ejecutive_comercial.xls") %>

  <%= link_to "Export", :controller=>"policy_management/policy",:action=>"generate_print_ejecutive_comercial" ,:format=>"xls",:search => params[:search],:page => params[:page],:filename=>"aaa.xls" %>

Here is my code from my controller:

respond_to do |format|
   format.html
   format.xls { render  :partial=>"report_by_ejecutive",:filename=>"aaa.xls"}
end 

Solution

  • I think render method does not do anything with the :filename option passed to it. However send_data does accept filename option but it needs to have the file as string. So using render_to_string to convert the partial to a string.

    format.xls { send_data render_to_string(:partial=>"report_by_ejecutive"), :filename => "aaa.xls" }