1

我有这样的链接

<%= link_to "download file", import_horse_report_horses_path(format: 'xlsx') %>

然后在控制器动作中我有以下代码

 def import_horse_report
    @horses = ImportHorse.all        
    respond_to do |format|
      format.xlsx{ }
    end
  end

对于这个动作,我有这个模板 import_horse_report.xlsx.axlsx

wb = xlsx_package.workbook
wb.add_worksheet(name: "Horses") do |sheet|
  @horses.each do |horse|
    sheet.add_row [horse.breed, horse.date_of_birth, horse.gender]
  end
end

我正在使用 axlsx_rails gem 并遵循其文档。但我得到这个错误

Missing template horses/import_horse_report, application/import_horse_report with {:locale=>[:en], :formats=>[:xlsx], :handlers=>[:erb, :builder, :coffee, :rabl]}

我该如何解决这个问题?

4

3 回答 3

1

您可以通过更改来具体尝试要渲染的 xls

format.xlsx{ }

format.xlsx{ filename =>'import_horse_report.xlsx' }

参考: https ://github.com/straydogstudio/axlsx_rails

于 2014-06-05T06:17:59.483 回答
0

我通过使用解决了这个问题

render xlsx: "import_horse_report" 

而不是respond_to。根据您的情况,这可能是一种选择。

于 2015-04-09T15:41:39.573 回答
0

尝试一个显式的渲染语句:

format.xlsx { render xlsx: 'import_horse_report' }

如果您在/horses/import_horse_report没有尾随的情况下调用,除非您在路由文件中强制格式,.xlsx否则将不会执行渲染语句。:xlsx如果您想保留您的respond_to阻止,请更改您的网址。否则,您可以删除该respond_to块以获得简单的render :xlsx语句(前提是您永远不会提供其他任何内容。)

于 2014-06-10T02:13:09.930 回答