Ruby on Rails:如何将文件呈现为纯文本(没有任何HTML) [英] Ruby on Rails: How to render file as plain text (without any HTML)

查看:93
本文介绍了Ruby on Rails:如何将文件呈现为纯文本(没有任何HTML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Rails中将文件呈现为纯文本/文本(不呈现任何HTML)?

How do you render file as a plain/text in rails (without rendering any 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.

But there still some html on the response.

我正在使用Rails 3.2.14和ruby 1.9.3

I'm using rails 3.2.14 and ruby 1.9.3

谢谢.

对不起,这实际上有效:

Silly me, this actually works:

render file: "path/to/file", layout: false, content_type: 'text/plain'

我正在使用google chrome检查该元素,因此出现了一个html标签.但是当我看到源代码时,没有HTML标记.

I was inspecting the element using google chrome, so there's an html tag appears. But when I see the source, there's no HTML tag.

我尝试渲染的文件是一个不带扩展名的定制文件,它是制表符分隔的值,我用作d3.tsv()(D3.js)中的参数

The file I was trying to render is a custom made file without extension, it is a tab separated value I use as a parameter in d3.tsv() (D3.js)

感谢大家的努力. :D

Thank you all for your efforts. :D

在Edit 1中,我检查了响应,的确没有任何html标签.但是在d3.tsv()(D3.js)中将其用作参数显然无效.很高兴我尝试了答案 JetAbe ,它起作用了!所以我接受了JetAbe的回答.

In Edit 1, I checked the response and it's true that it doesn't come with any html tag. But using it as parameter in d3.tsv() (D3.js) doesn't work apparently. Glad I tried the answer from JetAbe and it works! So I accepted JetAbe's answer.

推荐答案

我认为因为您的环境是rails 3,所以您可以尝试

I think because your env is rails 3 you can try this

send_file path_to_file

这篇关于Ruby on Rails:如何将文件呈现为纯文本(没有任何HTML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆