ruby-on-rails - Ruby on Rails : How to render file as plain text (without any HTML)

标签 ruby-on-rails file render plaintext

你如何在 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/

相关文章:

ruby-on-rails - 在Rails路线ID中有一个点

ruby-on-rails - 使用 declarative_authorization 保护敏感属性

javascript - 在 Python 和 Javascript 中渲染图形

文件扫描器循环不执行

node.js - nodejs + jade,我们可以像在 ASP.NET 中那样渲染部分 View 吗?

jquery - Renderblock JS 和 CSS 问题 - W3 Total 缓存插件

ruby-on-rails - 许多非常相似的功能,意大利面条代码修复?

mysql - 如何在 ActiveRecord 模型的属性中存储数组?

java - 解析 XML 截断文件路径

python - 使用输入文件对象从python中的文件中读取变量