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 Adding New Data Route Priority

Rhonda Goolsby
Rhonda Goolsby
6,992 Points

How to make this code correct?

I have no idea how to answer this question: I don't remember a video on random numbers and get calls.

fish.rb
require "sinatra"

get "/fish/:number" do
  "I see #{params[:number]} fish!"
end

get "/fish/random" do
  "I see #{rand(20)} fish!"
end

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 Points

I see you learned some Ruby, routing-- let's build on that.

Three things you need to make this work--

1 - you need to set up a binding to an ip address. This is required to get Sinatra to listen on an ip address other than localhost (127.0.0.1). You don't need this to develop on your local computer, but you do need this for TreeHouse workspaces.

set :bind '0.0.0.0'

2- you often need to have Sinatra listen to a port other than its default port of 4567. On TeamTreehouse, 8000 is one of those ports that you can expose to the outside world. (note: you can also omit this since TeamTreehouse also supports visibility on 4567)

set :port 8000

3 - Finally, you need to look at the URL routing order. In this case, you need to go from most specific to least specific. You have to have the /fish/random route come first, since that is a very specific URL. And after that you can have the fish route with the parameter. See below.

require 'sinatra'

set :bind, '0.0.0.0'
set :port 8000

get "/fish/random" do
    "I see #{rand(20)} fish!"
end 

get "/fish/:number" do
    "I see #{params[:number]} fish!"
end