Determine if a path exists as a route in a Rails controller

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

detected_request_for does what I want, but I cannot get it to work in my controller.

In particular, how can I perform a recognized_for_for or something that performs the same task from my controller?

+3
source share
5 answers

DECISION:

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

For Rails 3 Challenge

Rails.application.routes.recognize_path

Instead

ActionController::Routing::Routes.recognize_path

Example:

def path_exists?(path)
  begin
    Rails.application.routes.recognize_path(path)
  rescue
    return false
  end

  true
end
+23
source

, helper- , ( response_to? ).

0

, map.connect

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

:

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

, URL- params .

0

, , , . Rails.application.routes.recognize_path, rails 4.2.1. :

Rails.application.routes.named_routes.routes.any?{ |key, value| key.to_s === "new_article" }
0

Source: https://habr.com/ru/post/1755154/


All Articles