How do I execute a vote using the Rails 3 gem 'thumbs_up'?

1.7k views Asked by At

So I want votes to be triggered using jQuery/AJAX and an image. i.e. I have a thumbs up icon and thumbs down.

I have setup the gem, and ran the rake db:migrate and have everything sorted out.

But I am not sure how to execute a vote in my app.

In my model, I am using one model to acts_as_voter, which is my Client model. The model that acts_as_votable is my Upload model. So a client votes up/down an upload. It literally is a thumbs_up, not so much a 'vote' per se, because there is no karma or anything. It is simply a thumbs up/down from each client per upload.

In my view, this is how I would like to execute the vote:

$("div#upvote img").live("click", compv.comments.upvote);

How do I use this actually implement an upvote action using this Rails 3 gem ?

Edit: If you are interested in seeing what the function compv.comments.upvote does in it's current form, you can see it here:

    compv.comments.upvote = function(event){
    var uploaderID = compv.comments.getUploadID(event.target);
    $.ajax({
        url: '/uploads/'+uploaderID+'/upvote.js',
        success: function(data, status, xhr){
            var uploadElement = $("li[data-upload-id="+uploaderID+"]");
            uploadElement.attr('data-upload-upvote', parseInt(uploadElement.attr('data-upload-upvote'))+1);
            var imgElement = uploadElement.find("div.image-wrapper > img");
            var img_opacity = imgElement.css('opacity');
            if(img_opacity < 1 ) {  
                imgElement.fadeTo(600, 1, function() {
            }); 
            }
            imgElement.removeClass("downvoted");
            imgElement.addClass("upvoted");
        }
    });
};

However, I am well aware that this will likely have to change if I am to get this gem working right. That was just my version of trying to do what this gem can do (although, I would much prefer an implementation from this gem).

1

There are 1 answers

5
bouchard On BEST ANSWER

This is more a question of how to build Ajax actions in Rails in general than anything to do with this particular gem.

Referring to the setup here: Clarification on how to use "thumbs_up" voting gem with Rails 3

You can set this up in jQuery with something like:

$.post('/posts/' + id + '/vote_up', function() {
  alert('success');
})
.error(function() { alert('Vote error.'); });

Hopefully that gets you closer.