ruby - How do I get the route for a path, in a rails application? -


given method , path, want ask rails how request routed, , want able console, and/or rake task. figure should straightforward - actiondispatch every request, , route testing methods it.

(currently: rails 3.0.x, gem i'm writing need able in rails 3.0 through 4.1, @ minimum, , possibly in older versions well.)

i've been trying few things this:

routes = actiondispatch::routing::routeset.new routes.recognize_path('/my/path/23/edit') 

or

rails.application.routes.recognize_path('/my/path/23/edit') 

in both cases, "runtimeerror: route set not finalized".

i'm diving through actiondispatch code working out slowly, if knows answer off top of head save me considerable time. so, thank you!

the canonical answer given in question:

# request rails.application.routes.recognize_path('/some/path/63')   # post request rails.application.routes.recognize_path('/some/path', {:method => :post }) 

it looks wasn't working because i'd tried initializing routeset manually, had interfered environment's ability load routes. when restarted console session, above commands worked fine.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -