Platinum Partner

Rails Ajax Star Rater

Here's some Rails code I created to try out the CSS Star Rating Redux from Komodo Media.

I set up my routes.rb to use REST.

map.resources :titles, :member => { :rate => :any }

I use the Ruby Linguistics Framework to make this implementation easier. Install it using RubyGems (sudo gem install linguistics). And then put the following code at the top of your application_helper.rb 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, show.html.erb.

    <%= render :partial => '/partials/star_rating', :locals => { :record => @title } %>
Create a partial called _star_rating.html.erb in your RAILS_ROOT/views/partials directory.
  • Currently <%= record.rating -%>/5 Stars.
  • <% (1..5).each do |i| -%>
  • <%= 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" } -%>
  • <% end -%>
    {{ tag }}, {{tag}},

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}
    {{ parent.authors[0].realName || parent.author}}

    {{ parent.authors[0].tagline || parent.tagline }}

    {{ parent.views }} ViewsClicks
    Tweet

    {{parent.nComments}}