0

我觉得我应该知道这一点,我确信这可以更干净地完成,但我不太确定最好的方法。

像这样的一组路由如何以更 DRY 的方式编写?

# 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"
4

3 回答 3

1

我会尝试做 1 条路线并将 list_type 作为参数传递。

就像是

resources: artists do
  resources list_types
end

我会尽量避免对可能做类似事情的一堆方法采取单独的行动。

于 2012-10-07T17:17:48.093 回答
1

那应该这样做:

resources :artists, path: '/' do
  member do
    get 'remixes'
    get 'originals'
    get 'popular'
    get 'mashups'
    get 'covers'
    get 'features'
  end
end
于 2012-10-07T17:47:32.633 回答
0

啊,应该考虑一下(今天宿醉):

[:remixes_of, :remixes_by, :originals, :popular, :mashups, :covers, :productions, :features].each do |role|
  match ":id/#{role}", to: "artists\##{role}", as: "artist_#{role}"
end
于 2012-10-07T17:13:46.957 回答