[英]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.