Rails 3: how to render text file in-line?
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...
<pre>
<%= render :file => '/tmp/test.log' %>
</pre>
Here you go: http://edgeguides.rubyonrails.org/layouts_and_rendering.html
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.
精彩评论