Victor Castillo Torres
Victor Castillo Torres

Reputation: 10811

Get path helper from url, rails

I would like to know if there is a way to get the path helper from an url into a Controller something like:

def url_to_path(url)
       ########## CODE
end

url_to_path('/') #### root_path

Thank you in advance

Upvotes: 0

Views: 486

Answers (1)

Richard Jordan
Richard Jordan

Reputation: 8202

You can get the information on routes as follows:

Rails.application.routes.recognize_path("/", { :method => :get })

This will give you the controller, action and params. From that you can build the path names easily, if that's what you want.

If you want a list of all the named_routes helpers:

Rails.application.routes.named_routes.helpers

Upvotes: 1

Related Questions