Determine if path exists as route in Rails controller

后端 未结 5 1975
离开以前
离开以前 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:26

    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" }
    

提交回复
热议问题