Ruby on rails 替代嵌套的命名管线参数

Ruby on rails 替代嵌套的命名管线参数,ruby-on-rails,routes,Ruby On Rails,Routes,从Rails开始,您可以简单地执行以下操作: resources :videos, param: :identifier 但是,如果我有嵌套资源呢?以下是路线: resources :videos resources :images end 以上生成的路由如下所示: video_images GET /videos/:video_id/images(.:format) images#index 如何从路线覆盖:video\u id?在这种情况下,我似

从Rails开始,您可以简单地执行以下操作:

resources :videos, param: :identifier
但是,如果我有嵌套资源呢?以下是路线:

resources :videos
  resources :images
end
以上生成的路由如下所示:

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

如何从路线覆盖
:video\u id
?在这种情况下,我似乎无法使用
param

如果您想要
:identifier
而不是
:video\u id
,则必须手动对路线进行编码。这是一个痛苦,所以你真的应该考虑为什么你要在应用程序中使用非标准<代码> PARAM/<代码>值。
get 'videos/:identifier/images', to: 'images#index', as: 'video_images'
请注意,您需要对所有CRUD路线执行此操作

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"

我将尝试加强链接问题中未被接受的答案:

尝试将嵌套与
成员一起使用
-它将禁用父资源id的默认附加

然而这会使我们想要保留的东西出现问题,比如助手的名字。解决此问题的一种方法是使用
作为
(参见下面的示例)

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

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

因此,对于每一个其他可成像类,您必须添加另一个类似于上述的丑陋结构,但它至少应该保留其他内容并按预期行事。

原因是
图像
是多态的,我希望将
可成像id
作为标识符,而不是
视频id
音频id
或无论什么从
ImagesController
中查找父实体会更容易。但是谢谢你,我认为有一种方法可以在这个标识符上覆盖Rails魔法。你可以指定两次路由。。。嵌套在“视频”下,再次嵌套在“音频”下。然后在控制器方法中。。。特别是在实例化对象的
之前的操作中。。。你确实可以测试
params[:video\u id]
params[:audo\u id]
,但听起来不是很糟糕:)我会想其他方法,谢谢!