gpt4 book ai didi

ruby-on-rails - 覆盖嵌套的命名路由参数

转载 作者:行者123 更新时间:2023-12-03 16:21:07 25 4
gpt4 key购买 nike

来自 Rails docs ,可以简单地这样做:

resources :videos, param: :identifier

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

resources :videos
resources :images
end

上面生成的路由如下:

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

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

最佳答案

我将尝试增强链接问题中未被接受的答案:https://stackoverflow.com/a/29138733/2405850

尝试使用 member 的嵌套 - 它将禁用父资源 id 的默认附加。

但是这会给我们可能想要保留的东西带来问题,比如帮助者的名字。解决此问题的一种方法 - 使用 as(参见下面的示例)。

此外,我有类似的问题,仍然想要其他 url 的默认 id(例如,默认视频 CRUD 或视频是唯一可能的父级)。所以我终于想出了这个:

resources :videos do
resources :snapshots
end

resources :videos, param: :imageable_id, only: [] do
member do
resources :images, as: 'video_images'
end
end

resources :users, param: :imageable_id, only: [] do
member do
resources :images, as: 'user_images'
end
end

上面的代码将生成以下路由:

    video_snapshots GET    /videos/:video_id/snapshots(.:format)           snapshots#index
POST /videos/:video_id/snapshots(.:format) snapshots#create
new_video_snapshot GET /videos/:video_id/snapshots/new(.:format) snapshots#new
edit_video_snapshot GET /videos/:video_id/snapshots/:id/edit(.:format) snapshots#edit
video_snapshot GET /videos/:video_id/snapshots/:id(.:format) snapshots#show
PATCH /videos/:video_id/snapshots/:id(.:format) snapshots#update
PUT /videos/:video_id/snapshots/:id(.:format) snapshots#update
DELETE /videos/:video_id/snapshots/:id(.:format) snapshots#destroy
videos GET /videos(.:format) videos#index
POST /videos(.:format) videos#create
new_video GET /videos/new(.:format) videos#new
edit_video GET /videos/:id/edit(.:format) videos#edit
video GET /videos/:id(.:format) videos#show
PATCH /videos/:id(.:format) videos#update
PUT /videos/:id(.:format) videos#update
DELETE /videos/:id(.:format) videos#destroy
video_images GET /videos/:imageable_id/images(.:format) images#index
POST /videos/:imageable_id/images(.:format) images#create
new_video_image GET /videos/:imageable_id/images/new(.:format) images#new
edit_video_image GET /videos/:imageable_id/images/:id/edit(.:format) images#edit
video_image GET /videos/:imageable_id/images/:id(.:format) images#show
PATCH /videos/:imageable_id/images/:id(.:format) images#update
PUT /videos/:imageable_id/images/:id(.:format) images#update
DELETE /videos/:imageable_id/images/:id(.:format) images#destroy
user_images GET /users/:imageable_id/images(.:format) images#index
POST /users/:imageable_id/images(.:format) images#create
new_user_image GET /users/:imageable_id/images/new(.:format) images#new
edit_user_image GET /users/:imageable_id/images/:id/edit(.:format) images#edit
user_image GET /users/:imageable_id/images/:id(.:format) images#show
PATCH /users/:imageable_id/images/:id(.:format) images#update
PUT /users/:imageable_id/images/:id(.:format) images#update
DELETE /users/:imageable_id/images/:id(.:format) images#destroy

因此,对于所有其他可成像类,您将不得不添加另一个像上面那样的丑陋结构,但它至少应保留其他内容并按预期运行。

关于ruby-on-rails - 覆盖嵌套的命名路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34709734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com