0
我想将html erb表单作为html文件发送给用户的计算机。在我看来,我有两个按钮:“文本”和“html”。当他们点击“文本”按钮时,我已经成功发送了一个纯文本文件给用户的计算机,但是当他们点击“html”按钮时,它只会呈现erb表单,就好像它是一个视图。这里是我的控制器的相关部分:Rails 5:无法下载HTML erb表单
def download
@notecard = Notecard.find(params[:notecard_id])
file_title = @notecard.title.downcase.tr(" ", "_")
respond_to do |format|
format.html do
response.headers['Content-Type'] = 'text/html'
response.headers['Content-Disposition'] = "attachment; filename=#{file_title}.html"
render :template => "notecards/download"
end
format.text do
response.headers['Content-Type'] = 'text/plain'
response.headers['Content-Disposition'] = "attachment; filename=#{file_title}.txt"
render :template => "notecards/download"
end
end
end
我试过的处理“format.html做”块内部的逻辑,包括一些其他的方法:
send_data(content, filename: 'file.html', type: 'text/plain', disposition: 'attachment')
和:
render :template => "notecards/download", filename: 'file.html', type: 'text/html', disposition: 'attachment'
下面是在视图中按钮的代码:
= link_to "html", notecard_download_path(notecard_id: @notecard.id, format: "html")
我也尝试将内容类型更改为'text/plain'来欺骗它发送文件,这取决于我在其他地方收到的建议,但那也不起作用。
感谢您的答复。我只是尝试了更改,但它仍然只是将模板显示为页面。 – JGP
@JGP对不起,你应该可以删除':template =>“notecards/download”'。您已经在您的NoteCards控制器和下载操作中。看修改后的答案。 – user3366016
我很感激帮助。不幸的是,我仍然有同样的问题。任何想法为什么它仍然把它作为一个视图? – JGP