繁体   English   中英

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: []

希望你做得很好。 严格按照手册创建了一个新应用程序,并尝试使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM