ModelsController#index 缺少此请求格式和变体的模板。 request.formats: ["application/xlsx"] request.variant: []

ModelsController#index is missing a template for this request format and variant. request.formats: ["application/xlsx"] request.variant: []

希望你一切顺利。 我完全按照 this 手册创建了一个新应用程序,并尝试使用 axlsx_rails Gem 导出 excel 文件。这本手册看起来很好而且很完整,但是当我点击 line_to 标签时弹出这个错误

ProductsController#index is missing a template for this request format and variant. request.formats: ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"] request.variant: []

还有这个:

raise ActionController::UnknownFormat, message

我想这个错误与我的参数或类似的东西有关。 我正在使用 Rails 5 有什么想法吗? 一百万谢谢:)

编辑: 这是我的控制器:

def index
     @products = Product.order('created_at DESC')

     respond_to do |format|
      format.html
      format.xlsx {
        response.headers['Content-Disposition'] = 'attachment; filename="all_products.xlsx"'
      }
     end
  end

查看index.html.erb

<%= link_to 'Download as .xlsx', products_path(format: :xlsx) %>

查看index.xlsx.axlsx

  wb = xlsx_package.workbook
    wb.add_worksheet(name: "Products") do |sheet|
      @products.each do |product|
        sheet.add_row [product.title, product.price]
      end
    end

这就是解决方案 控制器:

def export_to_excel
    @tests = Test.all
      respond_to do |format|
        format.html
        format.xlsx {
          response.headers['Content-Disposition'] = 'attachment; filename="official_letters.xlsx"'
            }
    end
end

这是 浏览量/ ... export_to_excel.xlsx.axlsx

wb = xlsx_package.workbook
wb.add_worksheet(name: "tests") do |sheet|
  @tests.each do |test|
    sheet.add_row [test.title, test.price]
end