覆盖嵌套的命名路由参数 [英] Override nested named route parameters

查看:111
本文介绍了覆盖嵌套的命名路由参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Rails 文档,一个人可以轻松完成

Coming from the Rails docs, one can simply do this:

resources :videos, param: :identifier

但是,如果我嵌套了资源怎么办?以下是路线:

But, what if I have nested resources? Here are the routes:

resources :videos
  resources :images
end

上面生成的路线如下:

video_images GET  /videos/:video_id/images(.:format)                  images#index

如何我可以从路由中覆盖:video_id 吗?在这种情况下,我似乎无法使用 param

How can I override :video_id from the route? I can't seem to be able to use param in this case.

推荐答案

如果您要使用:identifier 而不是:video_id ,则必须手动编码路由。这很痛苦,因此您应该真正考虑为什么要在应用程序中使用非标准的 param 值。

If you want :identifier instead of :video_id you will have to code the routes manually. Which is a pain, so you should really consider why you want non-standard param values in your application.

get 'videos/:identifier/images', to: 'images#index', as: 'video_images'

请注意,您需要对所有CRUD路线执行此操作...

Note that you'll need to do this for all CRUD routes...

get 'videos/:identifier/images/:id', :to => "videos#show", :as => "video_image"
get 'videos/:identifier/images/new', :to => "videos#new", :as => "new_video_image"
post 'videos/:identifier/images', :to => "images#create"
get 'videos/:identifier/images/:id/edit', :to => "images#edit", :as => "ed it_video_image"
put 'videos/:identifier/images/:id', :to => "images#update"
delete 'videos/:identifier/images/:id', :to => "images#destroy"

这篇关于覆盖嵌套的命名路由参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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