2

I would simply like to display a .txt file located in my public directory onto a page. I apologise that this may seem novice but I am new to Ruby.

So far the ruby sinatra code reads:

get '/create' do
    logfile = File.open("logfile.txt")
    erb :create
end

and the erb reads:

<h2>Text file:</h2>
<%= logfile %>

Can someone tell me what I need to display this text file on my page?

samgbelton
  • 89
  • 1
  • 10
  • I got same type of smell here as well http://stackoverflow.com/questions/7997081/how-to-render-a-plain-html-file-with-sinatra – Rajarshi Das Mar 20 '14 at 12:27

4 Answers4

6

Another way to show .txt file with Sinatra(without erb).
in your script:

get '/' do
  send_file 'views/file.txt'
end

puts file.txt with content:

Heloo ! somebody here?


enter image description here

Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
  • How can this be printed in the `.erb` view page ? You are changing the OP's file system or project structure. The file is *public* folder. – Arup Rakshit Mar 20 '14 at 12:21
  • that is requested OP `Can someone tell me what I need to display this text file on my page?` – Roman Kiselenko Mar 20 '14 at 12:22
  • 2
    If what the OP wants is just the text, this is the simplest way to do it. If he wants to incorporate it as part of a page, it will require a view. – Mark Thomas Mar 20 '14 at 12:25
  • @MarkThomas I totally agree. Just another way. – Roman Kiselenko Mar 20 '14 at 12:27
  • People up voting you means,, I am not correct. Could you explain how your code displaying the text file, in the `.erb` view page ? I am really interested to get the explanations. – Arup Rakshit Mar 20 '14 at 12:49
  • @ArupRakshit there is no `.erb`, i just provide some another way `Displaying .txt file with Ruby Sinatra`.somebody can search this . – Roman Kiselenko Mar 20 '14 at 12:55
  • Ok... Please mention it in your answer, that your answer is another trick. Otherwise, future readers wouldn't be able to connect your answer with the post mention here. Thanks!! – Arup Rakshit Mar 20 '14 at 12:58
5

I think you were aiming for something like this:

get '/create' do
    @logfile = File.read("logfile.txt")
    erb :create
end

In the Erb:

<h2>Text file:</h2>
<%= @logfile %>

(Note that I would probably also put it inside a div with overflow: auto style applied)

Mark Thomas
  • 37,131
  • 11
  • 74
  • 101
3

You can do as below :

sinatra code :

get '/create' do
    @logfile = File.open("logfile.txt","r")
    erb :create
    @logfile.close
end

file.erb

<h2>Text file:</h2>
<% @logfile.each_line do |line| %>
  <%= line %>
<% end %>

Or you can use File#read :

file.erb

<h2>Text file:</h2>
<%= @logfile.read %>
Community
  • 1
  • 1
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • This keeps the file open through the entire HTTP request. This isn't necessarily a problem per se, but I personally get nervous when I don't see an explicit close. (I prefer block-form `open` or in this case `read` instead). – Mark Thomas Mar 21 '14 at 00:38
  • @MarkThomas Very good point indeed. I did it this way, as I don't know the file content, so I thought let the file pass to the erb as a object. Then inside the view, I will read the lines. Now point is, I need to close the file. If I put `@logfile.close` after `erb :create`, hope it would makes sense. Am I right ? – Arup Rakshit Mar 21 '14 at 06:39
0

Following this applied a secure solution as I need to output the text file inside my other HTML.

require 'rack/utils'

get '/logfile' do
    File.open('./public/log.txt','r') do |file|
        @logtext = Rack::Utils.escape_html(file.read) # this will assure security
    end
    erb :logfile
end

views/logfile.erb

<h1>Log file</h1>
<pre><%= @logtext %></pre>

Suppose our logs are less secure than our site code, and some bad person did a bad thing:

[INFO] User Admin successfully logged in.
[ERROR] User Frog failed to log in.
</pre><script>alert('Do harmful thing!')</script>
[INFO] User Admin successfully logged in.

The output will be exactly the above text (without the pop-up message).

Nick Legend
  • 789
  • 1
  • 7
  • 21