Modules and Routes
Posted by Guy Naor Wed, 01 Feb 2006 00:19:00 GMT
Here's a little something for rails novices. Might save you some time and pain.
I wanted nice URLs for my application, and one of the important parts was really short URLs for the main parts of the application. So you can do: http://www.famundo.com/library and get to the library module. Or http://www.famundo.com/photos to get to the library/photos area of the application. That's easy with routes. In routes.rb I just had to add entries like:
map.connect 'library' , :controller => 'library_center', :action => 'show'
map.connect 'photos' , :controller => 'my_pictures', :action => 'show'
map.connect 'blog' , :controller => 'personal_blogs', :action => 'show'
But my system has a further complication. I use modules to separate the different parts of the application, so that everything is nicely organized in development. So for my maps I used:
map.connect 'library' , :controller => 'library/library_center', :action => 'show'
map.connect 'photos' , :controller => 'library/my_pictures', :action => 'show'
map.connect 'blog' , :controller => 'library/personal_blogs', :action => 'show'
At first it seemed to work fine, but when I got deeper in to the controllers actions, the resulting URLs where all messed up. After some work and some help on the list, the solution was VERY simple. I was missing an initial slash in my routes! When referencing the controller inside the module, I needed to add a leading slash. So that the routes are now working with:
map.connect 'library' , :controller => '/library/library_center', :action => 'show'
map.connect 'photos' , :controller => '/library/my_pictures', :action => 'show'
map.connect 'blog' , :controller => '/library/personal_blogs', :action => 'show'
Ah, what a small {color:red}/ can do!

















