Rails - 强制下载XML模板而不是渲染

dia*_*ks2 3 xml ruby-on-rails

我的问题与这个问题非常相似,但这个答案对我不起作用.我有一个自定义的xml文件,我想下载而不是浏览器呈现.我尝试过这个send_file方法就像我上面链接的问题的答案,但我也得到一个错误can't convert Hash into String.

控制器:

respond_to do |format|
  format.tmx 
end
Run Code Online (Sandbox Code Playgroud)

模板:show.tmx.erb

<?xml version="1.0"?>
<tmx xmlns="http://www.gala-global.org/oscarStandards/tmx/tmx14b.html" version="1.4b">
</tmx>
Run Code Online (Sandbox Code Playgroud)

查看(我希望此链接在浏览器中下载文档而不是渲染):

 <%= link_to "Download", document_path(@document, format: "tmx") %>
Run Code Online (Sandbox Code Playgroud)

Nov*_*vae 7

您可以使用send_file,但是它需要引用已经设置的另一个提供内容的端点.或者,如果您不在任何其他上下文中使用xml,则可以使用以下命令:

format.tmx { send_data render_to_string(:show), filename: 'file.tmx', type: 'application/xml', disposition: 'attachment' }
Run Code Online (Sandbox Code Playgroud)

HTH,