Rails ActionController unknown format Rails ActionController unknown format ruby-on-rails ruby-on-rails

Rails ActionController unknown format


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.workbookwb.add_worksheet(:name => "Basic Worksheet") do |sheet|  sheet.add_row ["First Column", "Second", "Third"]end


The error you are getting doesn't mean that rails didn't find the the xlsx format: it means that it compared the list of formats you're providing (i.e. just xlsx) and compared it to the set of formats it thinks the browser is willing to accept and didn't find any overlap.

If as it appears there is only one format you want to render then you don't need to use respond_to at all - just replace the whole thing with

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

Rails derives what it thinks are acceptable formats from the extension on the url and the Accept header. Format negotiation is generally simply done via the extension than the Accept header - linking (or posting) to /some/path.xlsx should set the format to xlsx. You can do this by including format: 'xlsx' in the options you pass to path helpers or as part of a hash of routing options.


Well I am really late here, and the OP has probably moved on, but for others who've happened onto this problem, a possible explanation might be forgetting to set the format in the link itself. For example, you'd need to do something like this in your view: download_file_path(format: "xlsx")