Home arrow Ruby-on-Rails arrow URLs, Filters and the Rails Action Controller

URLs, Filters and the Rails Action Controller

In this third part of a four-part series on the Rails Action Controller, you will learn how to generate URLs dynamically, how to log with filters, and more. This article is excerpted from chapter four of the Rails Cookbook, written by Rob Orsini (O'Reilly, 2007; ISBN: 0596527314). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 15
February 07, 2008
  1. · URLs, Filters and the Rails Action Controller
  2. · 4.9 Inspecting Requests with Filters
  3. · 4.10 Logging with Filters
  4. · 4.11 Rendering Actions

print this article

URLs, Filters and the Rails Action Controller
(Page 1 of 4 )

4.8 Generating URLs Dynamically


There are many places in your code where you supply URLs to Rails methods that link to other parts of your application. You donít want to lose the flexibility Railsí Routes provide by hardcoding URL strings throughout your application, especially if you decide to change how routing works later. You want to generate URLs within your application based on the same rules that Routes uses to translate URL requests.


Use Action Controllerís url_for method to create URLs programmatically.


Letís say your default route (as defined in config/routes.rb) is as follows:

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

Then a call to url_for, such as:

  url_for :controller => "gallery",
:action => "view", :id => 4

produces the URL http://railsurl.com/gallery/view/4, which is handled by the default route. If you donít specify the controller, url_for assumes you want the current controller (the controller to which Rails delegated the current HTTP request).

This default behavior is useful because youíre often calling url_for to create a URL for another action in the current controller.

The same default behavior applies to the action and the ID: if you do not specify new ones, url_for defaults to the current one. Thus, for any of the components of the URL that you donít explicitly specify, Rails attempts to use values from the current request to construct a possible route mapping.

As soon as url_for finds one component that is different from the current request, it essentially slashes off all components to the right of it in the URL and no longer uses them as defaults. So, if you specify a different controller that of the current request, then neither the action nor any of the other parts of the current URL will be used to construct the new URL.

If the specified controller name begins with a slash, no defaults are used. If the controller changes, the action defaults to 'index' unless you specify a new one.

How the defaults work can get a little complicated, but url_for is usually intuitive. If youíre having trouble with unpredictable defaults, you can render the generated URL with render_text temporarily:

  render_text url_for :controller
=> "gallery", :action => "view", :id => 4

If you want to replace certain parts of the current URL without affecting any of the other parts of it, use the :overwrite_params option. For instance, if you want to change the current action to 'print', but keep the controller and the ID the same, use:

  url_for :overwrite_params => { :action
=> 'print' }

This takes the current URL, replaces only the :action, and returns it as the new URL.

See Also

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