你如何在 rails 中将文件渲染为纯文本/文本(不渲染任何 HTML)?
我试过了:
render file: "path/to/file", layout: false
和
render file: "path/to/file", content_type: 'text/plain'
也都
render file: "path/to/file", layout: false, content_type: 'text/plain'
但是响应中仍然有一些html。
我正在使用 rails 3.2.14 和 ruby 1.9.3
提前致谢。
编辑 1:
傻我,这实际上有效:
render file: "path/to/file", layout: false, content_type: 'text/plain'
我正在使用谷歌浏览器检查元素,所以出现了一个 html 标签。但是当我看到源代码时,没有 HTML 标签。
我试图渲染的文件是一个没有扩展名的定制文件,它是一个制表符分隔值,我在 d3.tsv() (D3.js) 中用作参数
谢谢大家的努力。 :D
编辑 2:
在编辑 1 中,我检查了响应,确实没有任何 html 标签。但是在 d3.tsv() (D3.js) 中使用它作为参数显然不起作用。很高兴我尝试了 the answer来自 JetAbe它有效!所以我接受了 JetAbe 的回答。
最佳答案
我想因为你的 env 是 rails 3 你可以试试这个
send_file path_to_file
关于ruby-on-rails - Ruby on Rails : How to render file as plain text (without any HTML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21129587/