Rails Unknown ActionController format

I am trying to display an xlsx file. But I keep getting 406/UnknowFormat . I did the right setup, maybe something is missing?

Rails 4.2 Application

 gem 'axlsx' gem "axlsx_rails" gem 'zip-zip' 

config / Initializers / mime

Mime::Type.register "application/xlsx", :xlsx

controller

 respond_to do |format| format.xlsx { render xlsx: "create", template: "api/reports/create" } end 

Views / APIs / Reports / create.xlsx.axlsx

 wb = xlsx_package.workbook wb.add_worksheet(name: "Reports") do |sheet| sheet.add_row [@report_name] end 
+6
source share
3 answers

The error you get does not mean that the rails did not find the xlsx format: it means that it compares the list of formats that you provide (i.e. just xlsx) and compared them with a set of formats, it thinks the browser is ready to accept and could not find matches.

If there seems to be only one format you want to display, you don’t need to use respond_to at all - just replace everything with

 render xlsx: "create", template: "api/reports/create" 

Rails gets what it thinks is an acceptable format from the extension on the url and Accept header. Format negotiation is usually done with an extension, than the Accept header - the binding (or publication) to /some/path.xlsx should set the format to xlsx. You can do this by including format: 'xlsx' in the parameters that you pass to the path helpers or as part of the hash of the routing parameters.

+4
source

For me in Rails 4.2, I needed to specify the fully qualified name of the template file, including the extension. According to the axlsx_rails docs , the syntax is different in Rails 4.2. Here is what worked for me:

some_controller.rb

 def create_report render "template_path/report.xlsx.axlsx" end 

template_path / report.xlsx.axlsx

 wb = xlsx_package.workbook wb.add_worksheet(:name => "Basic Worksheet") do |sheet| sheet.add_row ["First Column", "Second", "Third"] end 
+8
source

Well, I'm really late here, and the OP has probably progressed further, but for others who come across this problem, perhaps the explanation may forget to set the format in the link itself. For example, you need to do something like this in your view: download_file_path(format: "xlsx")

0
source

All Articles