Skip to content

Instantly share code, notes, and snippets.

@peteroome
Created November 15, 2009 00:49
Show Gist options
  • Select an option

  • Save peteroome/234881 to your computer and use it in GitHub Desktop.

Select an option

Save peteroome/234881 to your computer and use it in GitHub Desktop.
// application.js
$(function(){
$('body').removeClass('js-disabled').addClass('js-enabled');
$('#i_like_this').incrementILikeCount();
});
$.fn.incrementILikeCount = function() {
return this.each(function(){
var linkAction = $(this).attr('href')
$(this).click(function(){
var commentCount = $('h3#article_like_count span')
var linkText = $(this).parent();
$.get(linkAction,function(){
commentCount.text(parseInt(commentCount.text(),10)+1);
linkText.fadeOut('fast',function(){
$(this).text('Thanks for your vote');
});
linkText.fadeIn('fast');
});
return false;
});
});
}
// article_controller.rb
def like
article = Article.find(params[:id])
article.update_attribute(:likes, (article.likes + 1))
render :nothing => true
end
//articles => show.html.erb
<%= content_tag :h3, link_to("I like this!", {:controller => :articles, :action => :like, :id => @article}, :id => "i_like_this") %>
<%= content_tag :h3, pluralize_likes(@article.likes), :class => "comment_count", :id => "article_like_count" %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment