简体   繁体   中英

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. This manual seems just fine and complete but when I click on line_to tag this error pops up

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 ? 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

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

view 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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