Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What I did recently to add a simple rating mechanism to an existing project was the following: </p> <p>I added two fields to an existing table (which contained the items to be rated). Those were:</p> <pre><code>rating_score =&gt; The current score ratings =&gt; The number of ratings which led to the score </code></pre> <p>For example, if five users would've voted "5" for the current item, <code>rating_score</code> would be 25, and ratings would be 5. The current rating would be computed as <code>rating_score / ratings</code>.</p> <p>Then I added a new method to the controller of the items to be rated, called "rate", which looked something like:</p> <pre><code>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 do |format| format.js end end </code></pre> <p>My view for that method, called <code>rate.js.erb</code>, would look something like</p> <pre><code>$('#&lt;%= @container %&gt;').html('&lt;%= escape_javascript(render(partial: 'rating', locals: { item: @item })) %&gt;'); </code></pre> <p>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.</p> <p>And the <a href="http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials" rel="nofollow">partial</a> for the rating, called <code>_rating.html.erb</code>, was something like:</p> <pre><code>&lt;%= form_tag url_for(controller: 'items', action: 'rate', id: item.id), remote: true %&gt; &lt;%= rating_stars(item.rating_score, item.ratings) %&gt; &lt;%= item.ratings %&gt; Votes &lt;/form&gt; </code></pre> <p>In this partial, the <code>rating_stars()</code> helper method generated some kind of star-like representation for the rating, but you can do that however you like.</p> <p>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 <a href="http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/" rel="nofollow">unobtrusive javascript</a> thing going on in Rails lately, which is actually pretty cool.</p> <p>Hope this gives you an idea of how to realize a very simple rating system <strong>with no IP lock feature whatsoever</strong> in Rails.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload