Resource Rating

Created by Myriam Leggieri, @iammyr for Rails Girls Galway The basic guides that have been merged and adapted are the Ruby on Rails Tutorial, the basic RailsGirls app and the tutorials for creating thumbnails, authenticating users, adding design, deploying to OpenShift and adding comments.

What do we want our app to do? As a first thing, we would like to

We modeled and deployed comment, user and place resources, so far. Let’s now enable the rating for places.

Rating Places

Step 0: Add letsrate gem

Open up your Gemfile and add this line

gem "letsrate", :git => "git://github.com/iammyr/letsrate.git"

and run

bundle install

to install the gem. Also remember to restart the Rails server.

Step 1: Set up letsrate in your app

Run the following command in the terminal. (we are assuming that we had already enabled authenticated users using the devise gem)

rails g letsrate user

Step 2: Apply letsrate to your resource

You should add the letsrate_rateable function with its dimensions option, to the model of the resource you wish to rate, i.e., place. You can have multiple dimensions.

In app/models/place.rb add

  letsrate_rateable "autism_friendly", "overall"

Then you need to add a call letsrate_rater in the user model:

  letsrate_rater

Step 3: Render the Views

There is a helper method which name is rating_for to add the star links. By default rating_for will display the average rating and accept the new rating value from authenticated user.

Open app/views/places/show.html.erb and add

<p>
<strong>Votes:</strong><br>
Autism_friendly : <%= rating_for @place, "autism_friendly" %> <br>
Overall : <%= rating_for @place, "overall" %>
</p>
<hr />
<p>
<strong>Your votes:</strong><br>
Autism_friendly : <%= rating_for_user @place, current_user, "autism_friendly", :star => 7 %>
Overall : <%= rating_for_user @place, current_user, "autism_friendly", :star => 7 %>
</p>
<hr />

You can use the rating_for_user helper method to show the star rating for the user.

That’s it! ^__^ Try it out by restarting the server, add, commit and push on GitHub. If all it’s working then you can also deploy ;)