Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if path exists as route in Rails controller

I want to know if an arbitrary path can be mapped to a route

recognized_request_for accomplishes what I want, but I can't get it to work in my controller.

Specifically, how can I execute recognized_request_for or something that accomplishes the same task from my controller?

like image 585
Christopher Avatar asked Jul 17 '10 23:07

Christopher


People also ask

What is the difference between path and URL in Rails?

path is relative while url is absolute.

What is rake routes?

rake routes will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.


1 Answers

For Rails 3 the call is

Rails.application.routes.recognize_path

Instead of

ActionController::Routing::Routes.recognize_path

Example:

def path_exists?(path)
  Rails.application.routes.recognize_path(path)
  true
rescue ActionController::RoutingError
  false
end
like image 58
swrobel Avatar answered Nov 15 '22 08:11

swrobel