Home arrow Ruby-on-Rails arrow Action Pack Controller Creation

Action Pack Controller Creation

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

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
March 07, 2011
  1. · Action Pack Controller Creation
  2. · Generating a Controller

print this article

Action Pack Controller Creation
(Page 1 of 2 )

Configuring Routes for the Event Manager

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):

map.login 'login', :controller => 'users', :action => 'login'
map.logout 'logout', :controller => 'users', :action => 'logout'
map.signup 'signup', :controller => 'users', :action => 'new'

map.events     'events',         :controller => 'events'
map.new_event  'events/new',     :controller => 'events', :action => 'new'
map.edit_event 'events/edit/:id', :controller => 'events', :action => 'edit' map.event      'events/show/:id', :controller => 'events', :action => 'show'

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:

map.root '', :controller => 'events', :action => 'index'

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.

Static files, like those located in/public, are served by the web server independently of Rails. This means images, style sheets, and JavaScript files are all served in this manner, without any server-side processing, (which, by the way, is why we call them static).

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'

  map.login  'login',  :controller => 'users', :action => 'login'
map.logout 'logout', :controller => 'users', :action => 'logout'
map.signup 'signup', :controller => 'users', :action => 'new'

  map.events    'events',        :controller => 'events'
  map.new_event 'events/new',    :controller => 'events', :action => 'new'
  map.edit_event 'events/edit/:id'                :controller => 'events', :action => 'edit'
  map.event     'events/show/:id', :controller => 'events', :action => 'show'

   # Install the default route as the lowest priority.
  map.connect ':controller/:action/:id'

Now that we have some routes defined, let’s move on to creating the controllers and actions that will ultimately respond to these routes. Ready? Let’s go!

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials