Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Building Web Apps with Sinatra ERB Templates Loading Text Files

Adrian Filimon
PLUS
Adrian Filimon
Courses Plus Student 6,668 Points

I tried running the test however the console doesn't return anything.

So I did just like the video, creating a new directory, one txt file with my name and one rb called test. The problem is when i run the test on my console, i don't receive any message. Here is my code:

def page_content(title)
  File.read("pages/#{title}.txt")
rescue Errno::ENOENT
  return nil
end

puts page_content("Adrian")
caven xu
caven xu
Courses Plus Student 13,400 Points

Try these: 1.Check your directory structure; 2. Check your file name(same as 'Adrian') and file extension name(must be .txt)

alexx1000
alexx1000
16,187 Points

I tried running the test in my pc however the console doesn't return anything. So I did just like the video, creating a new directory, one txt file with my name and one rb called test. The problem is when I run the test on my console, I don't receive any message. Does anybody know why? Here is my code:

def page_content(title) File.read("pages/#{title}.txt") rescue Errno::ENOENT return nil end

puts page_content("Alejandra is cool : )")

I will appreciate your help! thank you so much!

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Is Adrian.txt within a pages/ subdirectory, like this?

.
|-- pages
|   `-- Adrian.txt
`-- test.rb

That's where it will look for the file, so if not, that's your problem. You should be able to confirm this by commenting out the rescue clause; then it will display an error if the file isn't there.

Marcos Rodriguez
Marcos Rodriguez
6,762 Points

When I comment out the rescue clause it says there is an error meaning it cant locate the .txt file right? but I clearly have test.rb and pages folder in the same directory and .txt file inside the pages folder. I made sure every spelling is correct. I put a puts "hello" inside the test.rb, and it passed through when I called it inside the console. What problem do you suggest I might have?

Jay McGavren
Jay McGavren
Treehouse Teacher

That's hard to know without seeing your code and your directory structure. Are you on Mac, Windows, or Linux? Can you paste the error you're seeing?

Marcos Rodriguez
Marcos Rodriguez
6,762 Points

Yeah my error is this

C:/Users/Marco/OneDrive/Desktop/produce/test.rb:2:in `read': No such file or directory @ rb_sysopen - pages/Marcos.txt (Errno::ENOENT)
        from C:/Users/Marco/OneDrive/Desktop/produce/test.rb:2:in `page_content'
        from C:/Users/Marco/OneDrive/Desktop/produce/test.rb:10:in `<main>'

I am using a windows and I have opened the command prompt from ruby. I am able to access the test.rb file. I guess the error is saying I dont have a Marcos.txt inside a pages folder but I do everything has the same spelling.

Jay McGavren
Jay McGavren
Treehouse Teacher

Marcos Rodriguez yes, that error indicates that your program either cannot find a pages folder in the current folder, or cannot find a Marcos.txt file within that folder.

If you type cd all by itself (without providing a directory name) on Windows, it should print the current folder path. What is the output of cd?

If you run dir /s, what is the output?

Jay McGavren
Jay McGavren
Treehouse Teacher

Marcos Rodriguez actually, depending on your version of Windows, it may not accept / as a file separator, only \. This course was created with the assumption students would run it in Workspaces, not on their local computers, so the code may not work correctly on a Windows machine.

Try using this code instead:

def page_content(title)
  File.read(File.join("pages", "#{title}.txt"))
rescue Errno::ENOENT
  return nil
end

puts page_content("Marcos")

If it works, you'll need to make that substitution in all the later code samples for the course. I would recommend using Workspaces instead, if possible.

Marcos Rodriguez
Marcos Rodriguez
6,762 Points

Hello, Jay I figured out the problem. To connect my file with ruby I would just click and drag my file inside the ruby command. But this didn't actually change directories to where I was supposed to be. I used cd to move into the folder that I had in my desktop. Now it works. It would be great to have a video on how to properly connect ruby with files on mac and windows. If there is one, please let me know. Thanks so much for your help!

Jay McGavren
Jay McGavren
Treehouse Teacher

Marcos Rodriguez we recently updated our Ruby Basics course, and this video has some details on how to make sure you're in the right directory and then run the right file.