Rails 3:如何内联渲染文本文件?

发布于 2024-11-14 10:21:49 字数 222 浏览 4 评论 0原文

全部。

这里有 Rails n00b...

我正在编写一个报告事务状态的应用程序。 渲染的 HTML 中的一些内容来自实例变量 在控制器中初始化,其他内容来自文本文件 (例如,日志文件)我想使用

 标签在 HTML 中呈现。

做到这一点的“Rails Way”是什么?

谢谢您的宝贵时间...

All.

A Rails n00b here...

I'm writing an application that reports the status of a transaction.
Some of the content in the rendered HTML comes from instance variables
initialized in the controller, while other content comes from text files
(e.g., log files) that I want to render in the HTML using <pre> tags.

What is the "Rails Way" to do this?

Thank you for your time...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹉夏雨初晴づ 2024-11-21 10:21:49
<pre>
    <%= render :file => '/tmp/test.log' %>
</pre>
<pre>
    <%= render :file => '/tmp/test.log' %>
</pre>
丢了幸福的猪 2024-11-21 10:21:49

在某些情况下(当文件不小且加载有延迟时),我更喜欢加载页面内容,然后使用 jQuery ajax 请求加载文件内容。

例如,假设我有一个具有文件路径属性的模型。在 view 布局中,我正在做这样的事情:

<pre data-source=" <%= (@file.path) %>"></pre>

然后在相应的 js 文件中,我将像这样加载上下文:

$(document).ready ->
  $.ajax(
    url: $("pre").data("source")
    context: document.body
  ).done (response) ->
    $("pre").html response
    return
  return

当然,您可以检查 jQuery ajax 文档了解更多选项。例如,您可以像这样使用 loading 渲染 pre 标签:

<pre data-source=" <%= (@file.path) %>"><div class="loading"></pre>

或者也使用其他 jQuery 动画。

In some cases (when the file is not small and loading it is connected with a delay), I prefer to load the page content and then to use jQuery ajax request to load the file content.

For example, let's say I have a model with file path attribute. In the view layout I am doing something like this:

<pre data-source=" <%= (@file.path) %>"></pre>

Then in the corresponding js file I am loading the context like this:

$(document).ready ->
  $.ajax(
    url: $("pre").data("source")
    context: document.body
  ).done (response) ->
    $("pre").html response
    return
  return

Of course you can check the jQuery ajax documentation for more options. For example, you can render the pre tag with loading like this:

<pre data-source=" <%= (@file.path) %>"><div class="loading"></pre>

or use other jQuery animations as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文