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.resources
call 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.root
method 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.resources
creates an astounding 14 routes, representing seven different methods built on REST, with and without a format.The
rake routes
command will show you the list of routes Rails believes it has.The
:anchor
parameter lets you specify a fragment identifier, which comes after # at the end of the URL.
Get Learning Rails now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.