5

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?

5 Answers 5

29

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
5
  • 1
    You shouldn't just be rescuing all Exceptions. Aug 24, 2017 at 13:10
  • 3
    @PRWhitehead instead of just downvoting and commenting negatively, how about suggesting which exception to rescue?
    – swrobel
    Aug 24, 2017 at 18:51
  • You can rescue from ActionController::RoutingError instead
    – moger777
    Jan 31, 2019 at 16:59
  • This doesn't work for redirects in route file unfortunately.
    – moger777
    Jan 31, 2019 at 19:17
  • Small clarification, rescue as used above is an implicit rescue of StandardError not Exception and is not the worst ruby crime you can commit. It conveys no meaning, but won't intercept a CTRL-C etc.
    – JP Duffy
    Jan 28, 2020 at 19:49
9

SOLUTION:

@related_page_path = '/' + @page.path
begin
  ActionController::Routing::Routes.recognize_path(@related_page_path, :method => :get)
rescue
  @related_page_path = nil
end
0
1

If you want to connect an arbitrary path the a controller and and action, you can use map.connect

map.connect '/any/random/string/of/stuff', :controller => 'items', :action => 'new'

You can even call out embedded param designations in the path:

map.connect '/seeking/room/for/[:number_of_nights]/nights', :controller => 'rooms', :action => 'index'

with the above you will receive the value represented in the url as part of the params hash in the controller.

1

I recently came across an issue where I had to check if a path existed given an array of possible paths. I had tried the above suggestion Rails.application.routes.recognize_path, but it's depericated as of rails 4.2.1. I used the following instead:

Rails.application.routes.named_routes.routes.any?{ |key, value| key.to_s === "new_article" }
1
  • In Rails 5.2.4.8, i'm getting private method `routes' called for #<ActionDispatch::Routing::RouteSet::NamedRouteCollection
    – ilasno
    May 19, 2022 at 23:46
0

You could possibly dynamically generate the route helper method and see if it exists (using respond_to? or even just catching any thrown exception).

2
  • yeah, I don't really know what that means :) Jul 18, 2010 at 1:17
  • 1
    @Cristopher say you are checking this from inside a controller you can use: self.respond_to?(:action_name). From a helper you can use controller.respond_to?(:action_name). Of course you can also inspect the rail routes with Rails.application.routes.url_helpers.respond_to?(:the_path_you_want_to_check)
    – microspino
    Sep 28, 2016 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.