In this fifth part of a multi-part series on the Action Pack library for Rails, you will learn how to configure routes for the event manager, and how to generate a controller. This article is excerpted from chapter six of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).
Let’s configure the routes we’re going to be using in our events application. We haven’t built all the controllers and actions yet (we’ll do that next), but that shouldn’t stop us from getting the routes in place. Openconfig/routes.rbin your editor. Start with the section that reads:
# Allow downloading Web Service WSDL as a file with an extension # instead of a file named 'wsdl' map.connect ':controller/service.wsdl', :action => 'wsdl'
We’re not using the Web Service WSDL in our application, so you can safely delete these lines. In the cutting-edge version of Rails, this definition has become extinct, so don’t be surprised if you don’t see it in the file you’re editing. Next, add the following routes, making sure they’re above the default route (the very last one in the file):
Take a minute to examine these definitions. They’re pretty straightforward. There’s a route to match the login URL, which will look like/login, as well as the/logoutroute, the/signuproute, and so on. When a URL comes in that matches any of these patterns, we’re forwarding it along to a specific controller and action. If the incoming URL matches none of these patterns, it will fall through to the default route (:controller/:action/:id), which will hand it off to the controller and action specified by the positional arguments in the URL, if they exist. If no controller or action that matches the URL exists, an error will be generated.
You can handle an empty request for the root of your application’s domain usingmap.rootand specifying an empty pattern to match. In our events application, we want the root URL (http://localhost:3000) to connect to the list of events. To accomplish this, we’ll add a route like the following and make it the first route:
Caution In edge-Rails,map.rootreceives a special designation and works without the empty pattern to match (""). If you get an error when usingmap.root, remove the empty pattern so the route looks likemap.root :controller => 'events', :action => 'index'.
The:action => 'index'parameter is actually redundant; taking a hint from most web servers, Rails assumes theindexaction if none is specified. Speaking of web servers, since the server looks for matching “static” files located in the/publicdirectory before ever invoking Rails, and since there’s a defaultindex.htmlfile there (it’s the Rails welcome page you saw in Chapter 2), we’ll need to delete it before our default route will take effect.
So, go ahead and deletepublic/index.htmlnow.
Listing 6-1 shows our final routes file, minus some of the comment clutter. Make sure yours looks just like this before you move on.
Listing 6-1. The config/routes.rb File
ActionController::Routing::Routes.draw do |map| map.root, '', :controller => 'events'