I am creating, some simple Ajax rating. Based on the first answer on this question Rails 3 rateable model - How to create ajax rating?
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.
How do I create the rating_stars
helper method to display the stars?
I would suggest you have a look at using the jQuery RateIt plugin.
It is entirely client side JavaScript and CSS. It also sports some sweet unobtrusive JavaScript and progressive enhancement. I've been pleased with it in my projects.
Once you've included jquery.rateit.min.js, rateit.css, delete.gif and star.gif into your public directories, you might have something like the following inside your rails form:
<%= f.text_field :ratings, :size => 1, :min => 0, :max => 5, :step => 1 %>
<div class="rateit" data-rateit-backingfld="#item_ratings" data-rateit-resetable="true">
</div>
The ID in data-rateit-backingfld
should match the ID produced by the text_field
form helper for your items field.
Clearly, this is using HTML5 data attributes, but if you're using Rails 3, then you'll be using HTML5 by default.