Search code examples
ruby-on-railsruby-on-rails-4routes

How to access route path from a Class method in a controller? (Rails)


I have a class method in application_controller.rb:

def self.method1
  path = some_path
end

Now it seems I can't access some_path since usually I'll need to use route in a controller (instance method).

What is the best way to access routes here?

Thanks


Solution

  • This should work from anywhere in your application

    path = Rails.application.routes.url_helpers.some_path