Rails ActionController unknown format

Seal picture Seal · Oct 29, 2015 · Viewed 12.9k times · Source

I am trying to render a xlsx file. But I keep getting a 406/UnknowFormat. I have done the right setup, maybe im missing something?

Rails 4.2 app

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/api/reports/create.xlsx.axlsx

wb = xlsx_package.workbook
wb.add_worksheet(name: "Reports") do |sheet|
  sheet.add_row [@report_name]
end

Answer

Patrick_870206 picture Patrick_870206 · Nov 13, 2015

For me, in Rails 4.2 I had to specify the full template filename including extension. According to the axlsx_rails docs the syntax is different in Rails 4.2. Here's 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