Generating Files

Migrating

Includes

Add the “rateable.css” and “rateable.js” to the includes in your layout
file

Usage

Model

Add to the user model that should be able to rate things:

classUser<ActiveRecord::Baseis_raterend

Add to the model that should be rateable

classModel<ActiveRecord::Baseis_rateableend

Controller

Add a rate action to the controller of the model that should be rateable,
it should look like this:

defrate# Check if the current user has already voted
ifcurrent_userandcurrent_user.ratings.where(:rateable=>@model).empty?@model.rate(current_user,params[:stars])render:partial=>"rateable/rating",:locals=>{:rating=>@model.ratings.average("stars").to_i}elserender:text=>"You have already voted for this item!",:status=>500endend

Routes

Add a post action called “rate” to the resource in your “config/routes.rb”

resources:modelsdomemberdopost:rateendend

Views

In your Views you can add the rating stars by adding:

<%= rating_for @model %>

You can also supply the url for the rate action manually, for example when
using nested resources

<%= rating_for @model, :url => url_for([:rate, @category, @model]) %>

If there is no current_user method you can also supply your current user
like this: