Using Rails 3 route helpers outside of a controller/view
I’m working on a job for delayed_job which sends an email including a link back to the site. I wanted to use Rails’s built-in URL helpers like new_post_path and edit_blog_url. After a little poking around I found this method call which gives you access to all the helpers from anywhere:
Rails.application.routes.url_helpers
Append your helper call to that method chain to get the final string:
Rails.application.routes.url_helpers.new_post_path
=> '/posts/new'
Rails.application.routes.url_helpers.edit_person_url(@person, :host => 'server.com')
=> 'http://server.com/people/3/edit'
If you use the _url version of the helper you’ll need to provide the host at the same time you include any other parameters that helper might need.