覆盖嵌套的命名路由参数

来自Rails 文档 ,人们可以简单地这样做:

resources :videos, param: :identifier 

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

 resources :videos resources :images end 

以上生成如下路线:

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

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

如果您需要:identifier而不是:video_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" 

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

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

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

此外,类似的问题我仍然希望其他url的默认ID(例如,默认videoCRUD或video是唯一可能的父级)。 所以我终于想出了这个:

 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 

所以对于其他每个可成像的类,你将不得不添加另一个像上面那样丑陋的结构,但它至少应该保留其他东西并按预期运行。