Test Your Knowledge
Quiz
How often does Rails reload the routes.rb file?
How do you set the routing for the empty URL, which is usually the home or landing page for a site?
If there are multiple routes that could match a given URL, how does Rails choose?
How do you tell Rails to just “grab the rest of this URL and put it into a parameter”?
How many routes does a single plain
map.resourcescall create?What’s the fastest way to see Rails’ list of routes?
How do you add a fragment identifier to the end of a URL created with
url_for?
Answers
In development mode, Rails checks to see if the routes.rb file has changed and reloads it if it has. In production mode, Rails doesn’t check, and you’ll need to stop and restart the server to update routes.
The
map.rootmethod lets you tell Rails how to handle requests aimed at the top of your site. You’ll also need to delete or rename public/index.html.Rails always applies the first route that matches a given URL, starting from the top of routes.rb.
Globbing, using an asterisk, lets you halt further processing of the URL and send it along to the controller method as a parameter.
map.resourcescreates an astounding 14 routes, representing seven different methods built on REST, with and without a format.The
rake routescommand will show you the list of routes Rails believes it has.The
:anchorparameter lets you specify a fragment identifier, which comes after # at the end of the URL.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access