Rails 3 使用视图渲染纯文本页面 [英] Rails 3 render plaintext page using view

查看:53
本文介绍了Rails 3 使用视图渲染纯文本页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 的数据库中存储了一个 Linux 配置文件,我希望能够通过网络请求下载配置

I have a Linux configuration file stored in my database in Rails, and I'd like to be able to download the configuration through a web request

我在 Linux 方面的目标是 curl/wget 网页,将其与当前配置进行比较,然后连接服务器.在脚本中很容易做到.

My goal on the Linux side is to curl/wget the webpage, compare it to the current configuration, and then hup the server. Easy enough to do in a script.

在 Rails 的正常情况下,你可以这样做

In normal circumstances on Rails, you could do

render :text => @config_file

但是,我需要先对数据进行一些格式化以应用静态标头等.这不是单行的,因此我需要能够呈现视图.

However, I need to do some formatting of the data first to apply the static headers, etc. This isn't a one-liner, so I need to be able to render a view.

我的控制器中有以下设置,但我仍然在文档中获得了最少的 HTML 标记集

I have the following set in my controller, but I still get a minimal set of HTML tags in the document

render(:content_type => 'text/plain', :layout => false);

我之前在 .Net 中做过类似的事情,所以它打印出一个带有 \n 解释的文本文件.我如何在 Rails 中获得它?

I've done something similar in .Net before, so it printed out a text file with \n interpreted. How do I get that in Rails?

推荐答案

通常,这是通过

# config/initializers/mime_types.rb
# ...
# Mime::Type.register "text/plain", :plaintext
# No changes needed as rails comes preconfigured with the text/plain mime type


# app/controllers/my_controller.rb

class MyController < ApplicationController
  def my_action
    respond_to do |format|
      format.text
    end
  end
end

和一个视图文件

# app/views/my_controller/my_action.text.erb
...

关于您在 DOM 中找到的最小 HTML:您是从某种浏览器内检查器(例如 google chrome 或 safari 中包含的检查器)中看到的吗?如果是这样,请不要担心,这是由浏览器添加的,以便内联显示您的文本/纯文本文档.如果您查看所交付文档的来源 (ctrl-u),则不应显示任何 HTML.

About the minimal HTML you find in the DOM: Are you seeing this from within some kind of in-browser inspector, like the ones included in google chrome or safari? If so then don't worry, this is added by the browser in order to display your text/plain document inline. If you look at the source of the delivered document (ctrl-u) no HTML should show up.

这篇关于Rails 3 使用视图渲染纯文本页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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