Reputation: 7911
This question is relatively simple but not quite local because it can be extrapolated to a lot of controller actions.
I have a games controller.
It (a game) has many planets.
So far I'm confused as to what belongs in each controller. I figure there must be a rails way to do it, which would be keeping planets actions in the planets controller, but I'm not sure how to do that.
EDIT (Note this isn't even gameplay yet):
To be more specific, here is a better description of an issue I'm stuck on. I'm not sure how to code this up (or if this is a very rails way to do it)
So in this example I understand the first two points, and the last point. But I don't understand if putting a redirect makes sense
Games Controller:
def play
game = Game.find(params[:id])
# do stuff
redirect planets_path(game)
end
Planets Controller:
def index(game)
@planets = game.planets
end
def show
@planet = Planet.find(params[:id])
end
The instance variables would be used in corresponding views. Also the planets_path would be linked to the planets index controller in the routes file.
Upvotes: 0
Views: 260
Reputation: 7196
Putting a redirect makes sense to me. Rails convention is to build resources in a RESTful way. So for the Game resource it would have an action which is 'play'. The action performs its logic (I assume that it would be setup logic for the Game) and then would direct the user to start the game itself. If this initial page is the list of available planets it makes sense to redirect them to planets_path.
Perhaps you should redirect to GameController#show? I would do this if there are other actions the user could perform after they start playing. Otherwise I would use this action for configuration and other information about the Game itself and have playable actions for the Game and Planet controllers.
If you make Planet a nested resource within a Game then the routes would be something like game_planets_path(game)
with a URL /games/1/planets
for PlanetsController#index
and game_planet_path(game, planet)
with a URL /games/1/planets/1
for Planetscontroller#show
. This helps with knowing that these planets belong to that game.
Upvotes: 1
Reputation: 83
A front-end js framework like backbones could be a solution.
It retrieves data in json format with back-end and then updates the views.
So that You could keep all planets' actions in planets controller, just implementing json-formatted respond as APIs.
Upvotes: 0
Reputation: 11940
Here Association Basics you will find examples for all association types in Rails and how to use them
Upvotes: 0