views:

343

answers:

3

I want to add a "like" functionality to a blog I am creating in Ruby on Rails to let people "thumbs up" a post. Then I would like to be able to display the most liked posts in the side bar. I am new to rails so rely heavily on tutorials and stack overflow. Any good resources or what is this even called. I assumed "voting" for this question.

+2  A: 

You should look up link_to_remote and using AJAX with rails. Here is a nice tutorial to get started: http://railsonedge.blogspot.com/2008/03/tutorial-beginning-ajax-with-rails-20.html

The idea would be:

  • The user clicks on "vote"

  • It sends an asynchronous message to the controller to save the vote. (use link_to_remote)

  • the :complete callback does something (eg "thanks for voting!")

  • the :update changes the voting zone (eg "voted. 6 votes on this")

There are a lot of tutorials on the subject.


EDIT: I wrote that a while ago and since then the best practices have changed. Now please take a look at how to do that using unobstrusive javascript. Good resources include Ryan Bates Bates's railscast #205 and the wikipedia article to understand the principle.

"Unobtrusive JavaScript" is an emerging technique in the JavaScript programming language, as used on the World Wide Web. Though the term is not formally defined, its basic principles are generally understood to include:

  • Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation

  • Best practices to avoid the problems of traditional JavaScript programming (such as browser inconsistencies and lack of scalability)

  • Progressive enhancement to support user agents that may not support advanced JavaScript functionality[

marcgg
Awesome man thanks. Just didn't know where to look. This helps a lot.
bgadoci
You're welcome. It's pretty hard to find documentation when begining. Consider reading a book about rails if you want to really grasp everything about it. I've listed some on an answer here: http://stackoverflow.com/questions/1386469/best-ruby-and-ruby-on-rails-books
marcgg
+2  A: 

I know this is not exactly what you are looking for, but there is a very good "star-rating" plugin that you could look at for some ideas of what to do. It uses ajax and all that goodie-goodieness ;)

Casper Fabricius
+2  A: 

The book Simply Rails 2 has something similar to this (a vote up, vote down sort of thing) and you might want to pick it up just because it is a good book to get you started on things.

Simply Rails 2.

Bob Martens