acts_as_trivia: would you like to play?
written on 05-18-2009 |
comments
We all love playing games, from poker to soccer, from hide'n seek to ... trivia! I am a great fan of this latter and also of Rails apps so it was kind of natural for me to come up with the idea of using all the data in your Rails app to serve as the basis of trivia questions.
The ingredients are few and basic. You have a class with an attribute that is comparable and makes sense to compare (the population of states is a good example, the zip code of addresses is a counter-example).
Installation
Install the gem
- Install the gem through Rails's config.gem
- Install it system-wide with the gem command
Put this:
config.gem "balinterdi-acts_as_trivia", :source => "http://gems.github.com",
:lib => "acts_as_trivia"
in your environment.rb and then run rake gems:install or rake gems:unpack.
gem install acts_as_trivia --source http://gems.github.com
Make your app act as a trivia
You just have to run the provided trivia from the root of your Rails application:
./script/generate acts_as_trivia
Create trivia questions easily from the command line
./script.generate acts_as_trivia_record country population name
This trivia question will be about the population of countries and the name of each will be used by the provided helpers for the sake of displaying something unique of each country.
Create the pages to answer and assess trivia
The acts_as_trivia gem only aims to provide a clean interface and the logic, so you will have to create the new controller action of the TriviaAnswersController and the corresponding view:
/users/:user_id/trivias/:trivia_id/trivia_answers/new
However, you can take a look at these files in the rails app generator or just directly copy them.
Get right in the game
Instead of going through the above steps, you can use a rails app generator to set up a trivia app quickly:
rails my_shiny_app -m http://gist.github.com/raw/107361/f31caad451f0cca699288700aa3d98291a259fd1/gistfile1.rb
Once you go through the setup steps of the app generator, you have everything you need to have a functioning trivia app, so you can go to:
/users/1/trivias/1/trivia_answers/new
And if you created model instances you can already answer your first trivia question. For a more technical (and more complete) description, please see the README. Also, since this is a beta version, please make sure to report any bugs you might come across.
