Page 1 of 1

Rails: Routes Explained - Part 1 A look at one of Ruby on Rails most useful features.

#1 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Post icon  Posted 07 February 2010 - 08:27 PM

Rails: Routes Explained - Part 1

Routes are basically how a Ruby on Rails application reads a URL. Routes can be modified by editing the file routes.rb found in the config folder of your Rails application. By changing this file you can set how URLs are handled by your application. One thing to note, however, is that the routes.rb file is prioritzed from top to bottom. Keep this in mind when planning out your routes.

Basic URL Routing

The most basic route, and by default already added to a Rails application, handles most of the URL calls you will receive:

map.connect ':controller/:action'
map.connect ':controller/:action/:id'



The first line tells Rails that for a URL like 'http://yourdomain.com/home/about' will access the 'home' controller and run the 'about' action within that controller. It basically breaks the URL down for your application. The first part of the URL after the domain name will be assigned as the controller and the second is assigned as the action.

The second line adds in the id parameter, which will be passed to the action as a parameter. An example of a URL that uses the second route would be 'http://yourdomain.com/users/show/1'. Since this URL has an added '/1' the first route will not work for it, so Rails will continue until it finds one that does, or error out if it doesn't. It will find that the next route can handle this URL. This is how the routes.rb file is prioritized.

Say that you get a URL like 'http://yourdomain.com/post/show/12' the route will broken it into:

params = { :controller => 'post',
	   :action => 'show',
	   :id => '12'
}



A problem with these default routes is that they're not often friendly for users and not very SEO at times. For example, if your login or logout actions are in a controller called user, then your users will have to access these with URLs like '/user/login' or '/user/logout'. You can simplify these URLs by setting a few routes with patterns:

map.connect '/login', :controller => 'user',
                      :action => 'login'
map.connect '/logout', :controller => 'user',
                       :action => 'logout'



Now a user can access the login and logout actions by using the URLs '/login' or '/logout'.

The '/login' tells the appliation what pattern to look for in a URL. In this case it's looking for 'http://yourdomain.com/login'. When the URL is accessed, the application now knows to route this request to the 'login' action in the 'user' controller. This may seem pointless to remove only one part of the URL, but the impact for the user is much larger than you think. Most users expect a URL like 'http://yourdomain.com/login' to let them login, but if you don't route it then it may try to access a controller named 'login' and the user will get a page not found error.


Setting a Default Page

You can also change the default root page of your application by adding the same map line with an empty URL pattern:

map.connect '', :controller => 'home',
                :action => 'index'



Now, if your application is accessed via just 'http://yourdomain.com/' your application will know to run the action 'index' from the 'home' controller. Of course, you would change the controller and action to whatever page you want visible. Just be sure to delete the index.html file from your public folder. By default, Rails will show this file as the root page if it exists.

Another option for specifying a default root page is the line:

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



Either of these two will work.


Making URLs Look Goooood

Another way to my your URL more friendly is by adding parameters into your route:

map.connect '/archive/:year/:month',
               :controller => 'archive',
               :action => 'list'



Here, we've added in 'year' and 'month' to the route. So, a URL like 'http://yourdomain.com/archive/2010/01' will route to the 'archive' controller and pass the year and month as parameters to the 'list' action. The list action can then use these parameters, along with some code, to retreive all blog posts from that month. It will be broken down into:

params = { :controller => 'archive',
	   :action => 'list',
	   :year => '2010',
	   :month => '01'
}




In closing,

As you can see, routing in Rails is as simple as a few lines of code. So far, we've seen how to set the default page of a domain, make a URL look more friendly, and tell the application how to route basic URLs. There are more ways that you can do routing in Rails and I encourage you to look into them. I will be covering more about routing in the next part.


Here are some things to try on your own:

  • How would you route the URL 'http://yourdomain.com/profile/2' to the 'show' action?
  • How would you route the URL 'http://yourdomain.com/about' to the 'home' controller?
  • How would you route the URL 'http://yourdomain.com/user/my_user_name' to pass the parameter 'username' to the 'show' action?



Thanks for reading. This is my first tutorial so I hope it was informative and helpful. Enjoy!

This post has been edited by Skaggles: 09 February 2010 - 03:21 PM


Is This A Good Question/Topic? 1
  • +

Page 1 of 1