Test Your Knowledge

Quiz

  1. How often does Rails reload the routes.rb file?

  2. How do you set the routing for the empty URL, which is usually the home or landing page for a site?

  3. If there are multiple routes that could match a given URL, how does Rails choose?

  4. How do you tell Rails to just “grab the rest of this URL and put it into a parameter”?

  5. How many routes does a single plain map.resources call create?

  6. What’s the fastest way to see Rails’ list of routes?

  7. How do you add a fragment identifier to the end of a URL created with url_for?

Answers

  1. 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.

  2. 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.

  3. Rails always applies the first route that matches a given URL, starting from the top of routes.rb.

  4. Globbing, using an asterisk, lets you halt further processing of the URL and send it along to the controller method as a parameter.

  5. map.resources creates an astounding 14 routes, representing seven different methods built on REST, with and without a format.

  6. The rake routes command will show you the list of routes Rails believes it has.

  7. The :anchor parameter lets you specify a fragment identifier, which comes after # at the end of the URL.

Get Learning Rails: Live Edition 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.