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
Written by Ryan McGeary
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#