map.resources :rants
map.rant_permalink "rants/:year/:month/:day/:permalink",
:controller => "rants",
:action => 'by_permalink',
:year => /\d{4}/,
:month => /\d{1,2}/,
:day => /\d{1,2}/,
:permalink => /\w(\w|-|_)+\w/
Generating URLs from a model (say for example rant) with a created_at field using the default rant_permalink_url helper would involve doing this:
rant_permalink_url :year => rant.created_at.year,
:month = rant.created_at.month,
:day = rant.created_at.day,
:permalink => rant.permalink
Very tedious to do that often. So I created a rant_permalink(rant) method (a wrapper for the rant_permalink_url function) in the ApplicationController, however this method isn’t accessible from the views. I put the method in the ApplicationHelper, but then it’s not available from the controller.
And then I scratched my head. I looked through the ApplicationController::Routing code and found that the “#{named_route_name}_url” functions are defined both in ActionController::Base and ActionView::Base. I thought about defining my methods both in the controller and the helper, but decided that wasn’t very DRY. I looked at the Rails doc entries for the helper method (which pulls in other helpers for use in your views) and noticed another method below it, helper_method this is an ActionController class method that aliases methods in a controller to methods of the same name in your helper. HURRAH!