0Pricing
Ruby Academy · Lesson

Routes and Params

Handling HTTP.

Handling HTTP Requests

Web apps respond to requests by matching routes and reading parameters.

  • Path segments can be dynamic
  • Query strings carry extra data
  • Form bodies carry posted fields

Sinatra exposes all of these through the params hash.

require 'sinatra'

get '/echo' do
  params['msg'].to_s
end

Named Route Parameters

A colon in the path defines a named parameter that captures a path segment.

  • /users/:id matches /users/42
  • The captured value appears in params['id']
require 'sinatra'

get '/users/:id' do
  "User ID is #{params['id']}"
end

All lessons in this course

  1. Sinatra Basics
  2. Routes and Params
  3. JSON APIs
  4. Middleware and Rack
← Back to Ruby Academy