Rails 3, Rake and url_for
April 29, 2010 7 Comments
Before I start I just want to make it clear that I know the arguments against using url_for in models and even in rake tasks. Sometimes however it makes sense to use_url for in a rake task. In my case I am trying to query another site’s api which requires the URI of the page on my site that I want to gather information about.
The approach in Rails 2.x
task :collect_stats => :environment do include ActionController::UrlWriter default_url_options[:host] = 'www.example.com' url = url_for(:controller => 'foo', :action => 'bar') end
Notice that because there is no current request you have to specify the
as the helper has no idea what the host will be otherwise.
Doing the same thing in Rails 3
The following code does the same thing in Rails 3.
task :collect_stats => :environment do include ActionDispatch::Routing::UrlFor #include ActionController::UrlFor #requires a request object include ActionController::PolymorphicRoutes include Rails.application.routes.url_helpers default_url_options[:host] = 'www.example.com' url = url_for(post) end
There are two key points to notice here.
- The first is that I have included ActionDispatch::Routing::UrlFor rather than ActionController::UrlFor. The latter requires a request object and will attempt to automatically fill in the host name. Since we are in a rake task there is no request and the method will fail.
- The second thing is that I have also included two additional includes. The will allow you to work with polymorphic routes and named routes, giving a bit more flexibility.
Just a short snippet that might be of use to people but if there are any improvements out there then please let me know and I will update this. You can of course hard code the routes but there are scenarios where it makes much more sense to make use of the helpers provided, especially when using polymorphic routes.
In the comments Jakub has stated that in the latest version of Rails you don’t need to include the polymorphic routes.