简体   繁体   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: []

Hope you're doing well. 希望你做得很好。 I followed exactly this Manual to create a new application and try exporting excel file using axlsx_rails Gem. 严格按照手册创建了一个新应用程序,并尝试使用axlsx_rails Gem导出excel文件。 This manual seems just fine and complete but when I click on line_to tag this error pops up 这本手册看起来还不错,但是当我单击line_to标记时,会弹出此错误

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

and this: 和这个:

raise ActionController::UnknownFormat, message

I guess this error relates to my parameters or something like that. 我猜这个错误与我的参数有关。 I'm using Rails 5 Have any idea ? 我正在使用Rails 5有什么想法吗? Thanks a million :) 太感谢了 :)

Edit: this is my controller: 编辑:这是我的控制器:

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

view index.html.erb 查看index.html.erb

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

view index.xlsx.axlsx 查看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

This is the solution Controller : 这是控制器的解决方案:

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

this is views/ ... export_to_excel.xlsx.axlsx 这是视图/ ... 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.

相关问题 ReporteController#opcionales缺少此请求格式和变体的模板。 request.formats:[“application / pdf”] request.variant:[] - ReporteController#opcionales is missing a template for this request format and variant. request.formats: [“application/pdf”] request.variant: [] ParksController#create 缺少此请求格式和变体的模板。 request.formats: [“text/html”] request.variant: [] - ParksController#create is missing a template for this request format and variant. request.formats: [“text/html”] request.variant: [] 为什么我无法解决错误“WelcomeController # index is missing a template for this request format and variant。 '? - Why can't I resolve the error ' WelcomeController # index is missing a template for this request format and variant. '? 在尝试呈现AJAX响应时“缺少此请求格式和变体的模板。” - “missing a template for this request format and variant.” when trying to render AJAX response 缺少此请求格式和变体的模板 - missing a template for this request format and variant 避免错误:缺少此请求格式和变体的模板 - avoiding the error: missing a template for this request format and variant 与Rspec销毁缺少此请求格式和变体的模板 - with Rspec destroy is missing a template for this request format and variant Rails测试“ ActionController :: UnknownFormat:缺少此请求格式和变体的模板” - Rails Test “ActionController::UnknownFormat: is missing a template for this request format and variant” Rails ajax聊天应用程序缺少此请求格式和变体的模板 - Rails ajax chat app missing a template for this request format and variant 使用[“application / javascript”]替换request.formats:[“text / html”] - Substitution of request.formats: [“text/html”] with [“application/javascript”]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM