我只想在页面上显示位于我的公共(public)目录中的.txt文件。抱歉,这似乎是新手,但我是Ruby的新手。
到目前为止,ruby sinatra代码显示为:
get '/create' do
logfile = File.open("logfile.txt")
erb :create
end
erb读为:
<h2>Text file:</h2>
<%= logfile %>
有人可以告诉我在页面上显示此文本文件需要什么吗?
最佳答案
您可以执行以下操作:
sinatra代码:
get '/create' do
@logfile = File.open("logfile.txt","r")
erb :create
@logfile.close
end
文件<h2>Text file:</h2>
<% @logfile.each_line do |line| %>
<%= line %>
<% end %>
或者您可以使用 File#read
:文件
<h2>Text file:</h2>
<%= @logfile.read %>
关于ruby - 使用Ruby Sinatra显示.txt文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22532554/