简体   繁体   English

我怎么能更干净地重写这组路线?

[英]How could I rewrite this set of routes more cleanly?

I feel like I should know this and I'm certain this could be done more cleanly, but I'm not quite sure the best way to go about it. 我觉得我应该知道这一点,我确信这可以做得更干净,但我不太确定最好的办法。

How could a set of routes like this be written in a more DRY way? 一个像这样的路线怎么能以更干的方式写出来呢?

# Artists
match "/:id/remixes", :to => "artists#remixes", :as => "artist_remixes"
match "/:id/originals", :to => "artists#originals", :as => "artist_originals"
match "/:id/popular", :to => "artists#popular", :as => "artist_popular"
match "/:id/mashups", :to => "artists#mashups", :as => "artist_mashups"
match "/:id/covers", :to => "artists#covers", :as => "artist_covers"
match "/:id/productions", :to => "artists#productions", :as => "artist_productions"
match "/:id/features", :to => "artists#features", :as => "artist_features"

I would look to try and do 1 route and pass list_type as a parameter. 我想尝试做1路由并传递list_type作为参数。

Something like 就像是

resources: artists do
  resources list_types
end

I would try and avoid having seperate actions for a bunch of methods that probably do similar things. 我会尽量避免对一堆可能做类似事情的方法采取单独行动。

That should do it: 应该这样做:

resources :artists, path: '/' do
  member do
    get 'remixes'
    get 'originals'
    get 'popular'
    get 'mashups'
    get 'covers'
    get 'features'
  end
end

Ah, should have just thought this through (hungover today): 啊,应该只是想到这一点(今天的悬念):

[:remixes_of, :remixes_by, :originals, :popular, :mashups, :covers, :productions, :features].each do |role|
  match ":id/#{role}", to: "artists\##{role}", as: "artist_#{role}"
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM