Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

In the wiki app, add a list of all the available wiki pages. The Dir class from Ruby core has an each method, Help=)

I'm trying to output the "pages" directory as navigation, please help =)

(from Ruby track, last part - Sinatra, the final lesson - from the teacher notes)


version 1

wiki.rb

def nav
d = Dir.new("pages")
d.each  {|x| puts "#{x}" }
end 

get "/" do
  @m = nav
  erb :welcome
end

welcome.erb

<ul>
<li><a href="/<%= Mackenzie Steele  %>"><%= @m %></a></li>
</ul>


version 2

def nav
d = Dir.new("pages")
d.each  {|x| puts "<li><a href=\"/#{x}\">#{x}</a></li>" }
end 

get "/" do
  @m  = nav
  erb :welcome
end

welcome.erb

<ul>
    <%= @m %>
</ul>

What am I doing wrong?

2 Answers

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

I'm ensuring a pages directory exists and then running the app, and I see what looks like a lone # sign. I assume these are the problems you're seeing also.

If I retrieve the page using the curl command (which may not be installed on your system), I get:

$ curl http://localhost:4567
<ul>
    #<Dir:0x00007f86c8a2c780>
</ul>

You'll also see something similar if you load the page in your browser and view the page source using the browser developer tools.

Here's what's happening:

  • Your nav method isn't returning a string, it's returning a Dir object.
  • That object is then converted to a string (<Dir:0x00007f86c8a2c780>) and rendered in your view.
  • Because <Dir:0x00007f86c8a2c780> is in angle brackets, the browser treats it as an HTML tag, one it has no idea how to render.

Instead, you need to return a string from the nav method. But if you've taken our Ruby Basics course, you've seen that puts doesn't return a string, it returns nil.

Instead, you need to render the text you want within the view, welcome.erb.

<ul>
        <% @m.each do |file| %>
           <li><a href="/<%= file.sub(/.txt/, '') %>"><%= file.sub(/.txt/, '') %></a></li>
        <% end %>
</ul>

Note that this will output the . and .. entries as well; I'll leave it as an exercise for you to remove those. (Maybe you could have nav return an array instead of a Dir.)

def nav 
d = Dir.new("pages") 
d.grep(/.txt/)
end
<ul>
  <% @nav.each do |file| %>
     <li><a href="/<%= file.sub(/.txt/, '') %>"><%= file.sub(/.txt/, '') %></a></li>
  <% end %>
</ul>

not sure it's the best way but it works, I tried to do something with .glob but I couldn't get into the "pages" dir.... =/

Thank you so much for your help Jay=))

Jay McGavren
Jay McGavren
Treehouse Teacher

Dan Maliano Nice! I like the use of grep and how its return value becomes the return value of nav. Very idiomatic!

Awesome! =) Thank you very very very much, Jay. I really appreciate your help and feedback.