Author-icon Posted By : Satish Chauhan @ 20 Aug, 2007 at 10:17PM

Recently I needed to implement star rating system for one of my project I am working on.

If you are not familiar with a star rating system, its simply a method of voting using (usually) 5 stars in a row, which will change colour as you hover over them indicating the level at which to rate something. You may think a simple rollover would accomplish this but difficulty arises because as you rollover each star it should stay highlighted while you light up the next one and so on until the end of the row of stars.

If this doesnt make much sense to you then take a look at what we are going to achieve in this article. (I will leave the server-side coding to someone more knowledgeable and we will just concentrate on the visual aspects of making this effect happen. )

As you can see from the demo the stars stay hovered as you rollover each one. However, as mentioned earlier, the complicated part is that as each star is hovered it should stay alight while the next one is hovered so that if you start at the left side then 1,2,3,4 and 5 stars will be alight as you traverse the row. Usually a rollover will only be active on the anchor that the pointer is currently over making previous anchors lose their focus and thus their hover effect disappear.



Install the acts_as_rateable plugin.

Run the following from the root of your Rails app to install the plugin.


script/plugin install http://juixe.com/svn/acts_as_rateable

or
http://rubyforge.org/projects/rateableplugin/

Create the table(migration) to store rating into database


class CreateRatings< ActiveRecord::Migration
def self.up
create_table :ratings, :force => true do |t|
t.column :rating, :integer, :default => 0
t.column :created_at, :datetime, :null => false
t.column :rateable_type, :string, :limit => 15, :default => “”, :null => false
t.column :rateable_id, :integer, :default => 0, :null => false
t.column :user_id, :integer, :default => 0, :null => false
end
add_index :ratings, [”user_id”], :name => “fk_ratings_user”
end
def self.down
drop_table :ratings
end
end

Model

models rateable

I was trying to add a rating system for the model Picture. Yours can obviously be whatever you like but from here on out I’ll be using Picture. So add acts_as_rateable to your model.


class Picture < ActiveRecord::Base
acts_as_rateable
...
end

Controller to handle the rating


class RatingController < ApplicationController


def rate
@picture = Picture.find(params[:id])
Rating.delete_all(["rateable_type = 'Picture' AND rateable_id = ? AND user_id = ?", @picture.id, current_user.id])
@picture.add_rating Rating.new(:rating => params[:rating], :user_id => current_user.id)
end
end

Views

Create the partial /views/rating/_rating.rhtml


<%= number_with_precision(picture.rating, 1) %>/5 Stars
< ul class='star-rating' >
< li class='current-rating' style='width:<%= (picture.rating * 30).to_i -%>px;’ >
Currently <%= number_with_precision(picture.rating, 1) %>/5 Stars.
< /li >
< li >
<%= link_to_remote( "1", {:url => { :controller => “rating”, :action => “rate”, :id => picture.id, :rating => 1}}, :class => ‘one-star’, :name => ‘1 star out of 5′) %>
< /li >
< li >
<%= link_to_remote( "2", {:url => { :controller => “rating”, :action => “rate”, :id => picture.id, :rating => 2}}, :class => ‘two-stars’, :name => ‘2 stars out of 5′) %>
< /li >
< li >
<%= link_to_remote( "3", {:url => { :controller => “rating”, :action => “rate”, :id => picture.id, :rating => 3}}, :class => ‘three-stars’, :name => ‘3 stars out of 5′) %>
< /li >
< li >
<%= link_to_remote( "4", {:url => { :controller => “rating”, :action => “rate”, :id => picture.id, :rating => 4}}, :class => ‘four-stars’, :name => ‘4 stars out of 5′) %>
< /li >
< li >
<%= link_to_remote( "5", {:url => { :controller => “rating”, :action => “rate”, :id => picture.id, :rating => 5}}, :class => ‘five-stars’, :name => ‘5 stars out of 5′) %>
< /li >
< /ul >

A little RJS

Create the file /views/rating/rate.rjs


page.replace_html “star-ratings-block”, :partial => rating/rating’, :locals => { :picture=> @picture }

This will replace the star ratings with the partial we created previously in order to reflect any rating changes made by the submission.

Render the partial in one of your views.


< div id="star-ratings-block" >
<%= render :partial => “rating/rating”, :locals => { :picture => @picture } %>
< /div >

This needs @picture(or whatver you’re going to be using) in order to function.

Remember to add the css and image


/* styles for the star rater */
.star-rating{
list-style:none;
margin: 0;
padding:0;
width: 150px;
height: 30px;
position: relative;
background: url(/images/star_rating.gif) top left repeat-x;
}
.star-rating li{
padding:0;
margin:0;
float: left;
}
.star-rating li a{
display:block;
width:30px;
height: 30px;
text-decoration: none;
text-indent: -9000px;
z-index: 20;
position: absolute;
padding: 0px;
}
.star-rating li a:hover{
background: url(/images/star_rating.gif) left center;
z-index: 2;
left: 0px;
border:none;
}
.star-rating a.one-star{
left: 0px;
}
.star-rating a.one-star:hover{
width:30px;
}
.star-rating a.two-stars{
left:30px;
}
.star-rating a.two-stars:hover{
width: 60px;
}
.star-rating a.three-stars{
left: 60px;
}
.star-rating a.three-stars:hover{
width: 90px;
}
.star-rating a.four-stars{
left: 90px;
}
.star-rating a.four-stars:hover{
width: 120px;
}
.star-rating a.five-stars{
left: 120px;
}
.star-rating a.five-stars:hover{
width: 150px;
}
.star-rating li.current-rating{
background: url(/images/star_rating.gif) left bottom;
position: absolute;
height: 30px;
display: block;
text-indent: -9000px;
z-index: 1;
}

Image for your CSS

0 Comments to "Star rating in ruby on rails"
No Comment posted yet

Leave a comment

( *required )
( *required )

Sponsored Links