1. Computing

Rails Blog Tutorial - Adding RESTful Authentication

By

5 of 8

Clean up the Routes

At this point, your routes.rb file will be a little messy. The original Rails generator created a routes.rb filled with comments and default routes that you do not need. The authenticated generator also created a route for the Users controller which no longer exists. This file should be cleaned up.

While you're in the routes.rb file, you can add several optional routes. To create some "cleaner" routes for the login and logout actions (technically the session "create" and "destroy" actions), named routes can be used. These named routes simply create the familiar /login and /logout URLs that will refer to the Sessions controller. A "root" route will also be created, telling Rails what to do if no controller or actions are defined in the URL. In order for the root route to work, the public/index.html file has to be deleted.

# File: config/routes.rb
 ActionController::Routing::Routes.draw do |map|
   map.resource :session
   map.resources :posts
 
   map.login '/login', :controller => 'sessions', :action => 'new'
   map.logout '/logout', :controller => 'sessions', :action => 'destroy '
 
   map.connect '', :controller => 'posts', :action => 'index'
 end
$ rm public/index.html
  1. About.com
  2. Computing
  3. Ruby
  4. Ruby on Rails
  5. Clean up the Routes

©2014 About.com. All rights reserved.