Rails beginner
Rails beginner

Reputation: 14504

Rails 3 rateable model - How to create ajax rating?

How do I create some simple ajax rating like there is on this page http://watir.com/documentation/ ? Every visitor should be able to rate, I dont need to set permissions. I want to store the ratings in a column. So the user can sort by ratings. Please make an detailled example. I am not a javascript expert.

I have found an example to create ratings from scratch. But it authorizes a user. Can someone show me a guidance to create ratings without a Rater (user)? It should not only store the values but also count the votes.

http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails-3

Upvotes: 2

Views: 1974

Answers (2)

fresskoma
fresskoma

Reputation: 25781

What I did recently to add a simple rating mechanism to an existing project was the following:

I added two fields to an existing table (which contained the items to be rated). Those were:

rating_score => The current score
ratings => The number of ratings which led to the score

For example, if five users would've voted "5" for the current item, rating_score would be 25, and ratings would be 5. The current rating would be computed as rating_score / ratings.

Then I added a new method to the controller of the items to be rated, called "rate", which looked something like:

def rate
    @item = Item.find(params[:id])
    @container = "item"[email protected]_s

    @item.rating_score += params[:rating].to_i
    @item.ratings += 1
    @item.save

    respond_to do |format|
        format.js
    end
end

My view for that method, called rate.js.erb, would look something like

$('#<%= @container %>').html('<%= escape_javascript(render(partial: 'rating', locals: { item: @item })) %>');

This code works only if you've got jQuery installed, but it should be easily translatable to Prototype or whatever JS framework you may be using.

And the partial for the rating, called _rating.html.erb, was something like:

<%= form_tag url_for(controller: 'items',  action: 'rate', id: item.id), remote: true %>
    <%= rating_stars(item.rating_score, item.ratings) %>
    <%= item.ratings %> Votes
</form>

In this partial, the rating_stars() helper method generated some kind of star-like representation for the rating, but you can do that however you like.

By setting "remote: true" in the form_tag helper, your Rails installation should automatically transmit the request via the installed Javascript framework. This magic is part of the whole unobtrusive javascript thing going on in Rails lately, which is actually pretty cool.

Hope this gives you an idea of how to realize a very simple rating system with no IP lock feature whatsoever in Rails.

Upvotes: 4

Jeremy Ruppel
Jeremy Ruppel

Reputation: 68

Looks like the Watir documentation rating system is set up through polldaddy.

For this particular case, it appears they include the polldaddy javascript which populates the rating div container with their star rating widget. Looks like there's a corresponding javascript object which you can inspect:

console.log( PDRTJS_101132_page_2.avg_rating ); //=> 4

If you had numerous rating widgets like these on a page, and you were able to make a collection of the javascript objects paired with their divs, presumably you could sort them based on that average rating property.

Upvotes: 0

Related Questions