0Pricing
Ruby Academy · Lesson

Routes and Params

Handling HTTP.

Routes and Params is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Multiple Parameters

A route can have several named parameters.

  • Each colon segment becomes a key in params
  • Order in the path does not matter for access
require 'sinatra'

get '/posts/:year/:month' do
  "Posts from #{params['month']}/#{params['year']}"
end

Query String Parameters

Query string values after ? also land in params.

  • /search?q=ruby gives params['q'] == 'ruby'
  • Multiple pairs are separated by &
require 'sinatra'

get '/search' do
  "Searching for: #{params['q']}"
end

Working with params as a Hash

The params object behaves like a Ruby hash. Practicing with a plain hash makes the patterns clear.

  • Use fetch with a default for safety
  • Keys are strings by convention
params = { 'name' => 'Ada', 'lang' => 'Ruby' }

puts params['name']
puts params.fetch('age', 'unknown')

Form Data with POST

For post requests, submitted form fields appear in params too.

  • Sinatra parses the request body automatically
  • Same access pattern as query params
require 'sinatra'

post '/signup' do
  "Welcome, #{params['username']}"
end

Splat Parameters

A splat (*) matches any number of characters and stores them in the params['splat'] array.

  • /files/* captures the rest of the path
  • Multiple splats produce multiple array entries
require 'sinatra'

get '/files/*' do
  "Path: #{params['splat'].first}"
end

Optional and Regex Routes

Routes can use regular expressions for precise matching.

  • Captures are available in params['captures']
  • Useful for constraints like numeric IDs only
require 'sinatra'

get %r{/item/(\d+)} do
  "Item #{params['captures'].first}"
end

Validating Parameters

Always validate and convert incoming params since they arrive as strings.

  • Use to_i or Integer() for numbers
  • Reject missing required values with a clear error
def parse_id(raw)
  id = Integer(raw)
  raise 'must be positive' if id <= 0
  id
end

puts parse_id('42')

Default Values

Provide sensible defaults for optional params to avoid nil errors.

  • params.fetch('page', '1')
  • Combine with conversion for safe pagination
params = { 'page' => nil }
page = (params['page'] || '1').to_i
puts "Page: #{page}"

Redirecting

Use redirect to send the client to another URL.

  • Issues a 302 status by default
  • Common after a successful form post
require 'sinatra'

post '/login' do
  redirect '/dashboard'
end

Quick Check

Check what you know about routes and params.

Recap

You learned how Sinatra handles input:

  • Named params use :name in the path
  • Query strings and form fields also populate params
  • Splats and regex routes capture flexible paths
  • Always validate and convert string params
  • Use redirect to move clients between URLs

Next you will return structured JSON responses.

Frequently asked questions

Is the “Routes and Params” lesson free?

Yes — the full text of “Routes and Params” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Routes and Params”?

Handling HTTP. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Routes and Params” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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