0

I have the models foo1, foo2, foo3 and foo4, which are subclasses of foo. And I have models like bar and baz.

Models bar and baz contain objects of type foo and of all its children. Also, each type has an action called next.

Now I must setup routes like

resources :bar do
  resources :foo do
    member do
      get :next
    end
  end
  resources :foo1 do
    member do
      get :next
    end
  end
  ...
end

I could use concern, to avoid setting the same to bar and baz, but I would still have to add get :next to every subtype of foo.

Is there some rails magic to do this that I'm not aware of?

Zach Kemp
  • 11,736
  • 1
  • 32
  • 46
pudiva
  • 274
  • 5
  • 15

1 Answers1

0

Presuming your question is about reducing duplication in the routes file, you can simply loop through arrays of keys:

[:bar, :baz].each do |parent|
  resources :parent do
    [:foo, :foo1, :foo2, :foo3, :foo4].each do |children|
      resources :children do
        member do
          get :next
        end
      end
    end
  end
end

However, that looks quite gross, so you may want to have another look at the design of your app and determine whether this is really necessary. Also, keep in mind that Rails expects a plural key as an argument to resources, so you may need to figure out what the plural of a key like :foo1 is (:foo1s, perhaps?).

Zach Kemp
  • 11,736
  • 1
  • 32
  • 46