Rails如何查看txt文件?
如何使用rails打开视图txt文件?
示例我有test.txt:
Test test test test
然后我有我的test.html.erb:
Here I want to view test.txt
您可以渲染任意文件或使用send_file
: 3.1或<3.1
另一种可能性是:
Here is the test.txt file: <%= File.read("test.txt") %>
在某些情况下(当文件不小并加载时会有延迟),我更喜欢加载页面内容,然后使用jQuery
ajax请求加载文件内容。
例如,假设我有一个带有文件路径属性的模型。 在view
布局中,我正在做这样的事情:
然后在相应的
js
文件中我加载上下文,如下所示:$(document).ready -> $.ajax( url: $("pre").data("source") context: document.body ).done (response) -> $("pre").html response return return
当然,您可以查看jQuery ajax文档以获取更多选项。 例如,您可以使用以下
loading
来呈现pre
标记:
或者使用其他
jQuery
动画。
这是test.txt文件:
<%= File.read(File.join(Rails.root, 'your_folder','your_folder','test.txt'))%>