4

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).

marcamillion
  • 32,933
  • 55
  • 189
  • 380

1 Answers1

3

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.

Community
  • 1
  • 1
bouchard
  • 820
  • 10
  • 27
  • That is with `/posts/` is the controller and `/vote_up` is the action right ? – marcamillion Feb 28 '11 at 08:10
  • So should I not make a `Votes Controller` but rather simply add a `vote_up` and `vote_down` action to the controller that the votes will be acted upon ? – marcamillion Feb 28 '11 at 08:13
  • Yup, that's certainly one way to do it. IMO, if you have votes on many different models, then make a unique votes controller. Otherwise, just have vote_up/down as actions on that one controller. – bouchard Feb 28 '11 at 08:16
  • Thanks. I think this does add some clarity - along with the other answer. – marcamillion Feb 28 '11 at 08:17
  • Hi, this is working for me, but i am not sure how to refresh the view to reflect the changes. – afxjzs Oct 31 '12 at 00:08