← Back to Upcase

Properly nesting controller actions


(Nicolo) #1

I’m trying to correctly create two index routes for a nested resource that are related to two resources.

I need one route that gives me all my lineups for a specific window.

Window has many Lineup

I need another route that gives me all my lineups for a given sport

Sport has many Windows and Windows have many Lineups

How do I setup the correct folder structure and routing to get this working in a proper restful rails way?


(Chad Pytel) #2

Hi @Nicolo,

I would probably create two different controllers, instead of making one generic controller that handles both.

But you don’t want to nest too deeply, so you don’t want to nest the Window lineups under a sport (unless there is no other way to know the sport)

So the directory structure would be:

  • app/controllers/windows/lineups_controller.rb
  • app/controllers/sports/lineups_controller.rb

And the routes would be

resources :windows do
   resources :lineups, controller: 'windows/lineups'
end

resources :sports do
  resources :lineups, controller: 'sports/lineups'
end

Let me know if I’ve misunderstood or gotten something wrong.

thanks,
-Chad


(Nicolo) #3

Great that helps a bunch. Do I need to use the Window or Sport prefix when defining the controller class?

For example:
Sports::LineupsController < ApplicationController


(Nicolo) #4

I think I just discovered the answer that question.

It does need to be:
Sports::LineupsController < ApplicationController