简体   繁体   中英

Ruby on Rails - Map URL to a controller, method, and parameters

I know that the file routes.rb in a Rails application maps a URL to a controller, a method, and a set of parameters in the params hash. This is complicated somewhat by "resources", nested resources, and non-RESTful routes.

Is there a way to run a command on the rails console to map a URL and figure out exactly which controller, which method, which http method(s), and the exact value of a parameter hash from a given URL?

Is it then possible to then run the controller method with the parameters hash on the rails console and get the output of the controller sent to STDOUT? If so, how?

Try these

Rails.application.routes.named_routes.each{|p,s| puts p,s} 
Rails.application.routes.url_helpers.my_path_helper   # To get url for a path helper

or

route = Rails.application.routes
route.recognize_path "/poweruser/3"           # will give you result exactly you wanted 

try it with running

bundle exec rake routes

See Determine if path exists as route in Rails controller

Rails.application.routes.recognize_path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM