Get the name of the current path in Rails

I would like to be able to get the name of the current route in the request in Ruby on Rails. I found ways to access the controller and the action of the request, but I would like to access the string or character of the name.

For example, if I have a resource users;

  • If I go to / users / 1, I would like to get users_path
  • If I go to / users / 1 / edit, I would like to get edit_users_path

I just want to get the name of the current route for this request.

+4
source share
2 answers

The following code will provide you.

Rails.application.routes.recognize_path(request.request_uri)

, , ActionDispatch::Routing::RouteSet ( , Rails.application.routes), . .

+2

URL- , , users_path

request.original_url # => www.mysite.com/users/1

request.request_uri # = > /users/1
0

All Articles