如何使用 Rails 3 gem“thumbs_up”执行投票?
所以我希望使用 jQuery/AJAX 和图像来触发投票。即我有一个竖起大拇指图标和一个竖起大拇指向下的图标。
我已经设置了 gem,并运行了 rake db:migrate 并将所有内容都整理好了。
但我不确定如何在我的应用程序中执行投票。
在我的模型中,我使用一个模型来 acts_as_voter
,这是我的 Client
模型。 acts_as_votable
的模型是我的 Upload
模型。因此,客户对上传进行投票赞成/反对。它实际上是一个竖起大拇指,而不是一个“投票”本身,因为没有业力或任何东西。这只是每个客户对每次上传的同意/反对。
在我看来,这就是我想要执行投票的方式:
$("div#upvote img").live("click", compv.comments.upvote);
我如何使用这个实际上使用这个 Rails 3 gem 来实现 upvote
操作吗?
编辑:如果您有兴趣查看函数 compv.comments.upvote
在当前形式中的作用,您可以在这里看到它:
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");
}
});
};
但是,我很清楚,如果我我要让这颗宝石正常工作。这只是我尝试做这个 gem 可以做的事情的版本(尽管,我更喜欢这个 gem 的实现)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这更多的是如何在 Rails 中构建 Ajax 操作的问题,而不是与这个特定的 gem 相关的问题。
请参阅此处的设置:澄清如何在 Rails 3 中使用“thumbs_up”投票 gem
您可以在 jQuery 中使用以下内容进行设置:
希望这能让您更接近。
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:
Hopefully that gets you closer.