簡體   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