Nathan Wienert
Nathan Wienert

Reputation: 1623

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"

Upvotes: 0

Views: 54

Answers (3)

Agis
Agis

Reputation: 33626

That should do it:

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

Upvotes: 1

Michael Durrant
Michael Durrant

Reputation: 96484

I would look to try and do 1 route and pass list_type as a parameter.

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.

Upvotes: 1

Nathan Wienert
Nathan Wienert

Reputation: 1623

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

Upvotes: 0

Related Questions