#79
Nov 11, 2007

Generate Named Routes

This more advanced episode will show you how to dynamically generate named routes by adding a method to the map object.
Tags: routing
Download (9.2 MB, 5:59)
alternative download for iPod & Apple TV (7.4 MB, 5:59)

Update: there’s now a plugin which does this called static_actions.

Resources

# routes.rb
ActionController::Routing::Routes.draw do |map|
  def map.controller_actions(controller, actions)
    actions.each do |action|
      self.send("#{controller}_#{action}", "#{controller}/#{action}", :controller => controller, :action => action)
    end
  end
  
  map.resources :products, :categories
  
  map.controller_actions 'about', %w[company privacy license]
end

RSS Feed for Episode Comments 45 comments

1. Brian Mitchell Nov 12, 2007 at 01:29

While I generally do use simple methods like you defined above on map, I will sometimes take a different approach for those types of urls.

I generally start applications by removing the :c/:a/:id style routes and add a "site" controller for general site topics. The routes file might look like this at first:

map.site ':action', :controller => 'site'

This works well enough since it can be used pretty cleanly:

site_path('jobs') #=> /jobs

There are some pros and cons here. One is that the actions can be added easily, which is a good thing in initial development... but if you like to strictly protect interfaces exposed on the web, then you can add some constraints on action:

map.site ':action', :controller => 'site', :action => /(jobs|sitemap|about)/

That regular expression will ensure only certain actions can get called ( the ()'s are required to correctly match given the |'s -- in 1.2 at least).

Finally, I should note that it is rather easy to add a formatted route as well:

# here is where things like with_options help a little.
map.site ':action', :controller => 'site'
map.site ':action.:format', :controller => 'site'

site_path('jobs') #=> /jobs
site_path('sitemap', 'xml') #=> /sitemap.xml

This might come in handy if you have several types of clients connecting to your service.

Of course, there are probably places where the overhead of these slightly more dynamic routes might be harmful.


2. Brian Nov 12, 2007 at 11:57

The one problem I keep having with routing is when you don't want the controller name in the route. For example, if you're doing a location based app, /texas/dallas/ is a lot more natural than /states/texas/cities/dallas. I do keep running in to a problem where two objects have the same scope, but you want to redirect to one if not found. i.e. /united-states/texas vs. /germany/munich. The way I've done this is ugly, but it works...kinda. Any suggestions would be helpful. I think there's a lot of voodoo with routing to most people.


3. Henrik N Nov 13, 2007 at 00:31

Brian: If worst comes to worst, you can have a catch-all route and do logic in a controller: http://railscasts.com/episodes/46

A very interesting, but pretty ugly/hackish way to use more powerful logic in your routes is http://www.ruby-forum.com/topic/42505.


4. lester bangs Nov 14, 2007 at 17:23

did you say "dick around" near the end there? ryan, i'm shocked! :)


5. James Nov 16, 2007 at 21:17

@lester - He said "dig around"! He has referred to "digging" in the past, so I am sure he is not a potty mouth. You have got your mind in the gutter! ;)


6. andré camargo Nov 16, 2007 at 23:40

Ryan, many thanks for every single episode. It's fantastic how you show the world of ruby, rails and the whole thing interact together. :-)

Keep your good work, thanks again!


7. Paul Barry Nov 19, 2007 at 06:32

It seems to me that this is meta just for the sake of being meta. Maybe that's just something you wanted to show in the screencast, but this seems like a cleaner way to do it:

%w{company privacy license}.each do |a|
  map.send "about_#{a}", "about/#{a}", :controller => 'about', :action => a
end


8. Ryan Bates Nov 20, 2007 at 10:20

@Paul, I agree this is an over-refactoring. It works best if you have quite a few of these kinds of actions in multiple controllers. That scenario was too complex for a simple screencast so I stuck with one controller.

In this case it's more about the technique then the circumstances to use it (which varies from project to project). I should have made that more clear.

@lester, LOL. I said "dig" around. I'll blame it on the cold I'm getting.


9. Drew Dec 06, 2007 at 12:43

Why create a method on map when you could simply send the actions to the map object?

def controller_paths(controller,*actions)
  actions.each do |a|
    map.send(#{controller}_#{a},#{controller/a},:controller => controller, :action => a)
  end
end

controller_paths :about, :company, :privacy, :liscense


10. Adrian Dec 11, 2007 at 01:19

A general question about routes:
Is there a naming convention between the route and the model relations?

model:
has_many usercomment
has_many usercomments, :through => usercomment

route:
map.resources :users do |users|
  users.resources :usercomments
end

Would this one work?


11. kino May 23, 2008 at 01:53

The Categories would thereby be made to contradict, consequently, the things in themselves.


12. Faktura Jun 15, 2008 at 00:25

Very useful cast!


13. Sukhwinder Singh Tambar Sep 30, 2008 at 01:21

Really usefull cast...


15. barcode freeware Nov 26, 2008 at 21:49

Enterprise barcode maker tool generate professional security tags or stickers.


16. Data Recovery Prices Nov 28, 2008 at 21:01

Flash card data recovery application restores audio video clips, pictures, documents.


18. Invoice program Dec 03, 2008 at 21:46

Providing the services of free invoice software to manage billing records of an enterprise


19. drv soft Dec 10, 2008 at 22:43

FAT partition data restoration program restore your all lost data from corrupted partition. Software fully supports all major hard disk drives of many brands including western digital, Samsung, Toshiba, Hitachi etc.


19. Data Recovery Reviews Dec 12, 2008 at 02:31

Free data recovery software services and solutions recover FAT/NTFS based partitioned hard disk drives and digital storage media files.


20. setup software Dec 16, 2008 at 01:21

Offers setup installer builder software to create installation files in executable (.exe).


20. Data Recovery Downloads Dec 18, 2008 at 03:34

Specialized in developing secure data recovery tool to restore data from all corrupted devices including iPod, zune etc.


21. file recovery tools Feb 12, 2009 at 23:55

Data recovery software free to automatically scan and rescue all accidentally deleted files and partition of hard drive due to virus attack on hard drive


22. kailas Feb 25, 2009 at 04:55

i'm trying to setup a route that's something like

map.connect ":foo/:controller/:action/:id", so i get :foo as a parameter.

that statement doesn't work in my routes.rb, but i'm a little confused about how to do it if it's possible at all (all the examples i've seen are of the form "controller"/"action"/[extra stuff]

any advice/suggestions would be greatly appreciated!

thanks!


23. file recovery Mar 24, 2009 at 05:37

Browse site about the data recovery software to recover deleted partition files of windows NTFS file system.


24. USB Port Blocker Mar 30, 2009 at 05:35

Download server USB monitoring software to protect images, database files from theft.


25. Barcode Generator Apr 08, 2009 at 05:19

Barcode labeling software creates barcode sticker or asset tags using special characters, numbers, upper or lower case letters. Barcode label printer tool make machine readable colorful barcode image, ribbon, sticker or security tags.


26. file recovery Apr 09, 2009 at 05:25

Website presents data recovery program that restores corrupted documents, images due to virus attack, software malfunction etc.


27. Keylogger Apr 28, 2009 at 02:43

Advance spy software captures screenshots of keyboard activities including file operations like open, create, delete and update etc


28. data recovery May 01, 2009 at 04:53

Data Recovery software which can easily recover your lost data in effective and efficient manner.


29. Recover files May 01, 2009 at 22:00

Software runs in invisible mode and prevents access of all commonly used USB drive media


30. tiffany-rings-c-69.html May 06, 2009 at 22:24

Really usefull cast!!


31. drive recovery May 15, 2009 at 00:44

Hard drive data revival tool restores files lost after improper system shutdown or hardware malfunction


32. Undelete Files May 18, 2009 at 05:47

Providing download for files undelete utility that quickly recover deleted iPod data when updated or restored using iTunes software.


33. file recovery software May 19, 2009 at 05:00

Company offers free file recovery software to recover data from all damaged musical devices like Zune, iPod etc.


34. Data recovery software May 28, 2009 at 03:02

Supplier of Digital photo recovery software salvages accidentally lost erased pictures, wallpaper, capture audio video clips and important snaps.


35. Repair partition May 28, 2009 at 23:28

Providing digital photo recovery program to restore pictures and video clips accidentally deleted from digital camera.


36. Database converter Software Jun 01, 2009 at 02:11

Offers MSSQL to MySQL DB converter to perform database migration process in fast and accurate manner.


37. Database converter Software Jun 01, 2009 at 02:11

Offers MSSQL to MySQL DB converter to perform database migration process in fast and accurate manner.


38. Data Recovery Jun 05, 2009 at 04:08

iPod data recovery software safely brings back your lost music, photos, audio/video files and supports all types of iPod models including iPod Nano, iPod Shuffle, iPod Classic etc.


39. restore iPod Jun 05, 2009 at 05:38

Focusing on data recovery utility that easily restores lost files from iPod digital media player.


40. restore iPod Jun 05, 2009 at 05:38

Focusing on data recovery utility that easily restores lost files from iPod digital media player.


41. sohbet siteleri Jun 10, 2009 at 12:03

thanks you


42. sohbet chat Jun 10, 2009 at 12:03

thanks you


43. chat siteleri Jun 10, 2009 at 12:03

thanks you


44. deleted recovery Jun 16, 2009 at 21:49

Company provides undetectable keylogger software that runs in background to capture and save screen images in a log file.


45. purchase order Jun 30, 2009 at 21:40

Developer of generic purchase order form generation software to manage sales and purchase records.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player