Accessing Named Routes in Rakefile
rails-routing, rake, ruby-on-rails
Solution
You can either use this example code in your rake task:
include Rails.application.routes.url_helpers
puts birthdays_url(:host => 'example.com')
or you can use this example code in your rake task:
puts Rails.application.routes.url_helpers.birthdays_url(:host => 'example.com')
If you only want the path part of the URL, you can use `(:only_path => true)` instead of `(:host => 'example.com')`. So, that would give you just `/birthdays` instead of `http://example.com/birthdays`.
You need either the `(:host => 'example.com')` or `(:only_path => true)` piece, because the rake task doesn't know that bit of information and will give this error without it:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
Problem
I have some named routes like this `rake routes`: ``` birthdays GET /birthdays(.:format) birthdays#index ``` In a rakefile I simply want to be able to call `birthdays_url` like I would in my view. ``` task :import_birthdays => :environment do url = birthdays_url end ``` But I'm getting an error `undefined local variable or method 'birthdays_url' for main:Object`