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 trialDan Maliano
12,142 PointsIn 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
Treehouse TeacherI'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 aDir
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
.)
Dan Maliano
12,142 PointsAwesome! =) Thank you very very very much, Jay. I really appreciate your help and feedback.
Dan Maliano
12,142 PointsDan Maliano
12,142 Pointsnot 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
Treehouse TeacherJay McGavren
Treehouse TeacherDan Maliano Nice! I like the use of
grep
and how its return value becomes the return value ofnav
. Very idiomatic!