DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Rails Ajax Star Rater
Here's some Rails code I created to try out the <a href="http://www.komodomedia.com/blog/2007/01/css-star-rating-redux/">CSS Star Rating Redux</a> from Komodo Media.
I set up my <b>routes.rb</b> to use REST.
map.resources :titles, :member => { :rate => :any }I use the <a href="http://www.deveiate.org/projects/Linguistics/">Ruby Linguistics Framework</a> to make this implementation easier. Install it using RubyGems (<b>sudo gem install linguistics</b>). And then put the following code at the top of your <b>application_helper.rb</b> file.
require 'linguistics' Linguistics::use(:en) # extends Array, String, and Numeric
This code goes in the page you want the rater to appear in, in this example, <b>show.html.erb</b>.
<ul class="star-rating" id="<%= dom_id(@title) -%>_rating"><%= render :partial => '/partials/star_rating', :locals => { :record => @title } %></ul>Create a partial called <b>_star_rating.html.erb</b> in your <b>RAILS_ROOT/views/partials</b> directory.
<li class="current-rating" style="width: <%= number_to_percentage((record.rating.to_f * 25) / (5 * 25) * 100) -%>;">Currently <%= record.rating -%>/5 Stars.</li>
<% (1..5).each do |i| -%>
<li><%= link_to_remote pluralize(i, 'Star'), {
:update => "#{dom_id(record)}_rating",
:url => eval("rate_#{record.class.name.downcase}_url(:rating => #{i})")
}, {
:class => "#{i.en.numwords}-#{i.abs == 1 ? 'star' : 'stars'}",
:title => "#{pluralize(i, 'star')} out of 5"
} -%></li>
<% end -%>




