Eki Eqbal
Eki Eqbal

Reputation: 6057

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

Upvotes: 4

Views: 2424

Answers (2)

Mohsin Mahmood
Mohsin Mahmood

Reputation: 3436

you may simply get the path using the following line of code

def foo
  path = request.path
end

Upvotes: 0

JTG
JTG

Reputation: 8836

This should work from anywhere in your application

path = Rails.application.routes.url_helpers.some_path

Upvotes: 9

Related Questions