When matching the path of a route, you can do it explicitly, matching only one path, like so:
get "/hello" do
return "Hello!"
end
You can also use a regular expression to match complex routes. Any route which matches the regular expression will run that code block. If multiple routes can potentially match the request, the first-matched route is executed.
Here's a typical example of a route that matches paths that include /user/
followed by one or more digits (presumably, user IDs) i.e. GET /user/1
:
get /\/user\/\d+/ do
"Hello, user!"
end
The example above matches /user/1
, but will also match /delete/user/1
and /user/1/delete/now
, since our regular expression is not very restrictive and allows for a partial match against any part of the path.
We can be more explicit with the regexp and tell it to match the route exactly, using \A
and \z
directives to anchor the match to the beginning and the end of the path:
get /\A\/user\/\d+\z/ do
"Hello, user!"
end
This route will not match /delete/user/1
or /user/1/delete/now
because of match anchoring.
Ignoring Trailing /
Our example route above will also not match /user/1/
(with trailing forward-slash). If you want to ignore trailing slash at the end of the route, adjust the regexp to make the slash optional (note the \/?
at the end):
get /\A\/user\/\d+\/?\z/ do
"Hello, user! You may have navigated to /user/<ID> or /user/<ID>/ to get here."
end
Capturing Route Matches
So far, we've matched against regexp routes, but what if we want to use the matched values in our code block? Following up on our example, how do we know what the user's ID is when the route is executed?
We can capture the desired part of the path and use Sinatra's param[:captures]
variable to work with the data inside the route:
get /\A\/user\/(\d+)\/?\z/ do
"Hello, user! Your ID is #{params['captures'].first}!"
end