覆盖“显示"Rails 中的资源路径 [英] Override "show" resource route in Rails

查看:44
本文介绍了覆盖“显示"Rails 中的资源路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

resources :some_resource

即有路由/some_resource/:id

事实上,some_resource:id 将始终存储在会话中,所以我想覆盖路径 /some_resource/:id使用 /some_resource/my.或者我想用 /some_resource/ 覆盖它并删除路径 GET /some_resource/ 以进行索引操作.

In fact, :id for some_resource will always be stored in session, so I want to override the path /some_resource/:id with /some_resource/my. Or I want to override it with /some_resource/ and remove the path GET /some_resource/ for index action.

我怎样才能达到这两个目标?

How can I reach these two goals?

推荐答案

在你的 routes.rb 中:

In your routes.rb put:

get "some_resource" => "some_resource#show"

resources :some_resource

然后 rails 会在找到资源之前获取您的get"...从而覆盖 get/some_resource

Then rails will pick up your "get" before it finds the resources... thus overriding the get /some_resource

此外,您应该指定:

resources :some_resource, :except => :index

尽管如前所述,rails 不会捡起来,但这是一个好习惯

although, as mentioned, rails won't pick it up, it is a good practice

这篇关于覆盖“显示"Rails 中的资源路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆