覆盖嵌套的命名路由参数

时间:2016-01-10 19:17:06

标签: ruby-on-rails routes

来自Rails docs,可以简单地执行此操作:

resources :videos, param: :identifier

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

resources :videos
  resources :images
end

以上生成的路线如下:

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

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

2 个答案:

答案 0 :(得分:2)

如果您想要: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"

答案 1 :(得分:2)

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

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

然而会使我们可能希望保留的内容出现问题,例如帮助者名称。解决此问题的一种方法 - 使用as(参见下面的示例)。

有类似问题我还需要其他网址的默认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

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

相关问题