My project is to make a website with html5 games, in Rails.
I have a controller called games_controller, and I would like to match /games/name-of-my-game with each html5 games. I created a route, matched with a games_controller function, but here is my problem:
I want everything to be “plug-and-play” for my html5 games, I mean develop them out-of-Rails, then just copy/paste it inside my app and everything works fine (references to images,.js files and all). But I also want my game’s view to be inside my application layout. Here is what I tried :
-
Put it in /public directory, and in my games_controller’s
showmethod dorender "/public/path_to_my_game". This breaks all references like<script type="text/javascript" src="some_script.js"></script>, cause the url isn’t */path_to_my_game* but /games/name-of-my-game. -
Put it in /public directory, but with a
redirect_toin myshowmethod insteadrender. The references are not broken, but the layout is gone (I may be wrong, but layouts are stuck with controllers, not static files). -
Put it in app/views/games but this seemed ugly so I quickly stopped :).
Does anyone have an idea ?
If you have something like
All you really have to do in your layout file is
This means you will have a
app/assets/javascripts/<game>.jsfile for each game you have. It will be responsible for booting each game.Obviously, you will want to take precaution to validate that
params[:game]is a valid game.Alternatively, you could create a content section in your layout file
Then serve the appropriate view with your
GamesControllerThen in your game views, you will require all necessary files for each game. For example, a user visits
/games/pacmanAnother example, user visits
/games/time_pilotBecause I’m sure each game is going to have a different amount of dependencies and they will all be initialized differently, you can build the view for each game accordingly and serve up whatever assets are relevant to each game.
Fore more help with with layouts and rendering, see the rails guide: Layouts and Rendering in Rails