Access Rails routes helpers outside the controllers e.g. in Models
Problem: The routes helper methods are available in views and controller as they are most of the time needed for displaying links in views or redirecting to other paths. But in one case I was expiring a page cache on an after_create callback of a model, for which I needed to access the path generated for that controller#action.
Solution: The RoutSet instance which contains all the helper methods is part of the Rails::Application object, and can be accessed at Rails.application.routes. You can access any URL helpers as follows:
Rails.application.routes.url_helpers.notification_path(notification) Rails.application.routes.url_helpers.notifications_path














