Wednesday, October 28, 2009

Act as Rateable

This plugin easily integrates a rating system in your Rails application. It allows users to create classifications in the model or models that you want. Imagine that you have a user and a photo model. Now you want to allow users to rate the photos, acts as rateable do all that for you. You will only need to personalize your views to reflect the changes and maybe use some AJAX to make it more user friendly.

So, to install the Acts As Rateable plugin run the following command:
script/plugin install http://juixe.com/svn/acts_as_rateable

You can found the rest of the instructions on http://www.juixe.com/techknow/index.php/2006/07/05/acts-as-rateable-plugin/

I have also added a new field in my rating migration to identify the content that has receive the classification.
To use this plugin, for instance if you want to create a classification you can do this:
post = Post.find(params[:id])
rating = Rating.new(:rating ==> 2, :submitted ==> Time.new)
@post.ratings << rating

This will rate the selected post with value 2.

If you want to know how much ratings does the post has:
post.ratings.size

To know the average classification of the post:
average_rating = post.rating

Note: Do not forget to add acts_as_rateable to the model you want to make rateable.
To have a much nicer experience you will need to have some JavaScript interaction with your rating system. There are a lot of free libraries that you can use which allows users to just click in a star and automatically recalculate the new rating average.

Cheers,
Hélder Loureiro

No comments:

Post a Comment