Basic app

Taking parameters from the url

Take a look at the following code:

# in app.rb:

require 'sinatra'

get '/' do
    "hello"
end

get '/:name' do
    name = params[:name]
    name
end

The /:name is a URL matcher. It will match / followed by any word, for example /tom, /gertrude or /this_isnt_a_name. Sinatra will make this value available in something called the ‘params hash’. You don’t need to worry about this at the moment. The first line of the block, pulls the value out of the params hash and sets it to the local variable name.

Exercise

Step 1: open app.rb in VSCode and uncomment the code at the bottom.

Step 2: make changest to the code so that visiting localhost:4567/tom shows “Hello Tom!” in the browser (and similarly for other names). [You will need to use string interpolation and a string method.]

Step 3: (Extension.) Check your result by running ruby test2.rb in the console.

Step 4: see if you can make it so that localhost:4567/tom/bye shows “Goodbye Tom” in the browser (and similarly for other names).

Step 5: (Extension - hard.) See if you can write another test in test2.rb to test your result.


Prev | Next