[英]How do you route root requests to a controller?
I have a controller called Videos. 我有一个名为“视频”的控制器。 I'd like my URLs to be like this: 我希望自己的网址是这样的:
http://website/1
Rather than this: 而不是这样:
http://website/videos/1
I'd also like to keep the functionality of controlling its content via /videos/
(edit, new, destroy, etc). 我还想保留通过/videos/
(编辑,新建,销毁等)控制其内容的功能。 Basically the show
action is the only one that would be responding from root. 基本上, show
动作是唯一会从root响应的动作。
Is there a way to do this without messing up with the other controllers? 有没有办法做到这一点而又不会弄乱其他控制器?
Just add a /:id
route, that links to videos#show
: 只需添加/:id
路由,该路由即可链接到videos#show
:
get '/:id' => 'videos#show'
You'll need to put this near the bottom of your routes file so that it doesn't match requests for things like /videos
. 您需要将其放置在路由文件的底部附近,以使其与/videos
类的请求不匹配。
You can create all RESTful routes for a controller, without its name in path by using 您可以使用以下命令为控制器创建所有RESTful路由,而无需在路径中添加其名称
resources :videos, path: "" 资源:视频,路径:“”
This will create all routes for videos controller like (index, new, edit, create etc) 这将为视频控制器创建所有路由,例如(索引,新建,编辑,创建等)
If you want only a specific route to serve as this then you can pass that too with this like 如果您只希望使用特定的路线作为路线,那么您也可以像这样通过
resources :videos, path: "", only:[:show] 资源:视频,路径:“”,仅:[:显示]
This will create only your desired path 这只会创建您想要的路径
http://website/1 http:// website / 1
and will not affect any other controllers. 并且不会影响任何其他控制器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.