Quickstart guide

rubyrage edited this page Jan 30, 2012 · 6 revisions

Steps

1. Install

gem install ajaxful_rating

2. Generate

script/generate ajaxful_rating <YourAlreadyCreatedUserModelClassName>

The generator takes only one argument, which is the name of your user model. For this of course, you should have already created it.

Also, this command will generate the next necessary files:

  • app/models/rate.rb
  • db/migrate/xxxxxxxxxxxx_create_rates.rb
  • public/images/ajaxful_rating/star.png
  • public/images/ajaxful_rating/star_small.png
  • public/stylesheets/ajaxful_rating.css

Don’t forget to run the migration:

rake db:migrate

3. Prepare

Within the <head></head> tags of your layout, include the next lines:

<script src="http://gist.github.com/330057.js?file=application.html.erb"></script>

You need to add a member to your model rest route, for example if you want your Car model be rateable:

<script src="http://gist.github.com/330057.js?file=routes.rb"></script>

And finally, add the action method to handle the ajax request in your cars controller:

<script src="http://gist.github.com/330057.js?file=cars_controller.rb"></script>

The explanation of the methods used here can be found in the Ajaxful controller page.

4. Use it

To add a model the capability of receive ratings, add the following line in your model class:

<script src="http://gist.github.com/330057.js?file=car.rb"></script>

And in your user model, the ability to rate resources:

<script src="http://gist.github.com/330057.js?file=user.rb"></script>

And in your views, to display the nice hover stars:

<script src="http://gist.github.com/330057.js?file=show.html.erb"></script>

That’s it.

If you want a model to receive ratings for different “features”, like a car can be rated by its speed, price and beauty, you could use the :dimensions option in the ajaxful_rateable call. This option is explained at detail in the Dimensions page.