简体   繁体   English

Ruby on Rails - 将URL映射到控制器,方法和参数

[英]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. 我知道Rails应用程序中的文件routes.rb将URL映射到params散列中的控制器,方法和一组参数。 This is complicated somewhat by "resources", nested resources, and non-RESTful routes. “资源”,嵌套资源和非RESTful路由有些复杂。

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? 有没有办法在rails控制台上运行命令来映射URL并确切地确定哪个控制器,哪个方法,哪个http方法,以及给定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? 然后可以在rails控制台上使用参数hash运行控制器方法,并将控制器的输出发送到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控制器中的路由

Rails.application.routes.recognize_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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