Determine if path exists as route in Rails controller

后端 未结 5 1978
离开以前
离开以前 2021-01-13 03:57

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.

5条回答
  •  臣服心动
    2021-01-13 04:22

    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
    

提交回复
热议问题