This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | Thu Jul 17 08:50:36 -0700 2008 | |
| |
README.textile | Wed Jun 10 05:53:25 -0700 2009 | |
| |
Rakefile | Thu Jul 17 08:50:36 -0700 2008 | |
| |
generators/ | Tue Jun 09 13:28:31 -0700 2009 | |
| |
init.rb | Thu Jul 17 11:02:00 -0700 2008 | |
| |
install.rb | Thu Jul 17 08:50:36 -0700 2008 | |
| |
lib/ | Tue Jun 09 13:28:32 -0700 2009 | |
| |
tasks/ | Thu Jul 17 08:50:36 -0700 2008 | |
| |
test/ | Thu Jul 17 08:50:36 -0700 2008 | |
| |
uninstall.rb | Thu Jul 17 08:50:36 -0700 2008 |
is_rateable
Currently star buttons are a put request, not a post request like they should be due to time constraints. Should be fixed in a future version.
Patches welcome! I do test my own stuff but until I find a nice and easy way to include rspec in plugins, they won’t come with the specs.
Usage
To install, type script/generate rateable [--by_user]
Options
—by-user will associate your ratings to a user model. Hopefully you’ve got one already.
h4. Without Ajax In your model.
is_rateable :upto => 5 # it defaults :upto => 5
In your view.
render_rating(@book, :interactive_stars)
render_rating(@book) # If you want to also see 1/5 Stars
If a user rates more than twice, it will delete their older rating and put up a new rating.
Add a method to your books_controller like so:
def rate
@book = Book.find(params[:id])
unless @book.add_rating(params[:rating].to_i, :ip => request.remote_ip)
flash[:notice] = "You have already voted on this book"
end
redirect_to @book
end
Using Ajax
You can use the same code for your model
is_rateable :upto => 5 # it defaults :upto => 5
On your view, you need to call a partial
#Using html.erb
<div id="rating">
<%= render :partial => "books/rate", :locals => { :rateable => @book } %>
</div>
#Using haml
#rating
render :partial => "books/rate", :locals => { :rateable => @book }
Your partial will have a spinner and a div to be replaced when ajax is completed
#Using html.erb (views/books/_rate.html.erb)
<%= render_ajax_rating rateable %>
<%= image_tag "icons/spinner.gif", :id => :spinner, :style => "display: none;" %>
<div id="success_rating" style="display:none;">
Rated!
</div>
#Using haml (views/books/_rate.html.haml)
= render_ajax_rating rateable
= image_tag "icons/spinner.gif", :id => :spinner, :style => "display: none;"
#success_rating{:style => "display:none;"}
Rated!
On your controller:
def rate
@book = Book.find(params[:id])
@book.rate params[:rating].to_i
render :update do |page|
page.replace_html("rating", {:partial => "rate", :locals => {:rateable => @book}})
page.show "success_rating"
end
end
Todo
- Annotate methods
Credits
- Zach Inglis
- Nathan Arnold
- Les Hill
- Matt Remsik
- Tim Pope
- Rogie King – CSS for Stars
- Some code from restful_authentication by Rick Olson
Ajax Rating
Copyright© 2008 Zach Inglis, released under the MIT license












