Rails 3 rateable model - How to create ajax rating? -
how create simple ajax rating there on page http://watir.com/documentation/ ? every visitor should able rate, dont need set permissions. want store ratings in column. user can sort ratings. please make detailled example. not javascript expert.
i have found example create ratings scratch. authorizes user. can show me guidance create ratings without rater (user)? should not store values count votes.
http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails-3
what did add simple rating mechanism existing project following:
i added 2 fields existing table (which contained items rated). were:
rating_score => current score ratings => number of ratings led score
for example, if 5 users would've voted "5" current item, rating_score
25, , ratings 5. current rating computed rating_score / ratings
.
then added new method controller of items rated, called "rate", looked like:
def rate @item = item.find(params[:id]) @container = "item"+@item.id.to_s @item.rating_score += params[:rating].to_i @item.ratings += 1 @item.save respond_to |format| format.js end end
my view method, called rate.js.erb
, like
$('#<%= @container %>').html('<%= escape_javascript(render(partial: 'rating', locals: { item: @item })) %>');
this code works if you've got jquery installed, should translatable prototype or whatever js framework may using.
and partial rating, called _rating.html.erb
, 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 partial, rating_stars()
helper method generated kind of star-like representation rating, can like.
by setting "remote: true" in form_tag helper, rails installation should automatically transmit request via installed javascript framework. magic part of whole unobtrusive javascript thing going on in rails lately, pretty cool.
hope gives idea of how realize simple rating system with no ip lock feature whatsoever in rails.
Comments
Post a Comment