Last Updated: February 25, 2016
·
382
· rmm5t

Rails Routing URLs Anywhere

This is something that ends up in the app/lib directory of almost every Rails project that I manage. Credit goes to Adam Hawkins's original blog post on the topic, but the code here is slightly modified to be used as a singleton.

# Router.instance.posts_url  
# => http://localhost:3000/posts
# Router.instance.posts_path 
# => /posts
class Router
  include Singleton
  include Rails.application.routes.url_helpers

  def self.default_url_options
    ActionMailer::Base.default_url_options
  end
end