Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current path name in Rails

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

For example, if I have a users resource;

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

I simply want to retrieve the name of the current route on a given request.

like image 818
Dwight Avatar asked Oct 20 '14 12:10

Dwight


People also ask

How do I get the current URL in Ruby?

You should use request. original_url to get the current URL.

How do I find routes in Rails?

TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

What is namespace in Rails routes?

This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.

What is match in Rails routes?

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

What does the path_names option do?

The :path_names option lets you override the automatically-generated new and edit segments in paths: This would cause the routing to recognize paths such as: The actual action names aren't changed by this option. The two paths shown would still route to the new and edit actions.

How do I see all of my routes in bin/rails?

Both methods will list all of your routes, in the same order that they appear in config/routes.rb. For each route, you'll see: For example, here's a small section of the bin/rails routes output for a RESTful route:

How do I test a custom route in rails?

Routes should be included in your testing strategy (just like the rest of your application). Rails offers three built-in assertions designed to make testing routes simpler: assert_generates asserts that a particular set of options generate a particular path and can be used with default routes or custom routes. For example:

How to prevent name collisions between routes in rails?

You can use the :as option to prefix the named route helpers that Rails generates for a route. Use this option to prevent name collisions between routes using a path scope.


1 Answers

The following code will give it to you.

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

Note that there are a couple of exceptions that can get thrown in ActionDispatch::Routing::RouteSet (which is what is returned from Rails.application.routes) so be careful about those. You can find the implementation of the method here.

like image 146
jjbohn Avatar answered Oct 06 '22 21:10

jjbohn