2014-06-05 33 views
1

我有这样Axlsx-Rails的宝石不能正常工作,显示缺少模板

<%= 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宝石并按照其文档。但我得到这个错误

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

我该如何解决这个问题?

+0

您保存了这个'import_horse_report.xlsx.axlsx'模板的位置?路径? –

+0

在这个控制器的其他动作的视图 – asdfkjasdfjk

+0

让我的路径 –

回答

0

尝试一个明确的渲染声明:

format.xlsx { render xlsx: 'import_horse_report' } 

如果您在调用/horses/import_horse_report而没有结尾.xlsx,则除非您在路由文件中强制使用:xlsx格式,否则不会执行渲染语句。如果您想保留respond_to区块,请更改您的网址。否则,你可以删除respond_to块一个简单的render :xlsx语句(前提是你永远不会投放任何东西。)

+0

有一个非常简单的可能性。你在叫'/ horses/import_horse_report.xlsx'还是'/ horses/import_horse_report'?如果是后者,则不会在不使用'routes'强制格式的情况下找到模板。 – noel

+0

编辑我的答案。 – noel

0

我用

render xlsx: "import_horse_report" 

,而不是固定的respond_to这个。这可能是一个选项,取决于你的情况。