Adding user rating feature using letsrate
To engage your site user is to make your web application interactive. Interactive could lead to many things. For example: allow your user to rate your content.
In this post I will show how to add that feature using one nice gem called letsrate.
Installation
Add letsrate into your Gemfile then run bundle install as usual. The letsrate gem need Rate and RatingCache models linked to your existing User model. You can accomplish this using the generator. Please note here that I am using devise to manage authentication in my application. The official documentation also using devise. If you try to use letsrate using other authentication gem, please share your experience.
rails g letsrate user
Because letsrate generate some migration files as well, you want to do this
rake db:migrate
Integration
Now I would like to let my user to rate other user's posts
# app/models/post.rb class Post < ActiveRecord::Base letsrate_rateable "quality" end # app/models/user.rb class User < ActiveRecord::Base letsrate_rater end
Next is to add the star links to post show view.
<p> Quality: <%= rating_for @post, "quality" %> </p>
That's it, now go visit your post show page. You will see there is star links to rate your content quality.
It goes without saying that letsrate is more than you read in this post. So go ahead, add it to your application and make fun of it while you explores its features.