[英]Is there an easy way to route an entire controller to a site root?
I know how to route a page to a site root, and how to individually specify routes for every single page and action, but I feel like I am creating unnecessary work for myself at times by doing this. 我知道如何将页面路由到站点根目录,以及如何为每个页面和每个动作分别指定路由,但是我觉得自己有时会为此做一些不必要的工作。 Is it possible to make it so that
example.com/method
always equates to some_controller#method
? 是否有可能使
example.com/method
始终等于some_controller#method
?
Something like: 就像是:
root "some_controller#index"
controller :some_controller do
get "/:method" => :method
end
instead of 代替
root "some_controller#index"
controller :some_controller do
get "/" => :index, as: :index
get "/contact" => :contact, as: :contact
get "/photos" => :photos, as: :photos
...
end
or is that the best way? 还是最好的方法?
You're pretty close already: 您已经很接近了:
controller :some_controller do
get ":controller/:action"
end
:controller/:action
will dynamically match the :action
segment to a method of that name on the controller referenced by the symbol passed to the controller
method. :controller/:action
将:action
段与传递给该controller
方法的符号所引用的控制器上具有该名称的方法动态匹配。
I would be careful about security here. 我会在这里注意安全性。 Take note on what methods your controller inherits from its parent class (probably ApplicationController), as any public methods will be accessible using this routing style.
请注意您的控制器从其父类(可能是ApplicationController)继承的方法,因为任何公共方法都可以使用此路由样式进行访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.