[英]acts_as_votable javascript html table
我正在尝试在Rails中使用ruby中的acts_as_votable宝石。 但是,当我需要将按钮添加到视图时会卡住。
我想在用户在Web浏览器中填写表格时将投票系统添加到放置在html表中的项目列表中。
我可以在每个“目标”项目的每一行中插入一个“喜欢”按钮。 但是,这是否意味着我需要编写一个“ attachLikeHandler”函数来处理单击“赞”按钮时发生的情况?
这是dashboard.js:
var insertDest = function(dest) {
// Find a <table> element with id="myTable":
var table = document.getElementById("destTable");
// Create an empty <tr> element and add it to the 1st position of the table:
var row = table.insertRow(1);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
var name_cell = row.insertCell(0);
var address_cell = row.insertCell(1);
// var delete_cell = row.insertCell(2);
var like_cell = row.insertCell(2);
like_cell.innerHTML = '<input class="Like" type="button" value="Like" />';
var like_count_cell = row.insertCell(3);
like_count_cell.innerHTML= 0;
// Add some text to the new cells:
name_cell.innerHTML = dest.name;
address_cell.innerHTML = dest.address;
// delete_cell.innerHTML = "<div class='del'>x</div>";
addMarker(dest.address,map);
};
var insertAllDest = function(trip){
var d = trip.destinations;
for (i in d){
insertDest(d[i]);
}
};
但是,这是否意味着我需要编写一个“ attachLikeHandler”函数来处理单击“赞”按钮时发生的情况?
是:
$(".like").on('click', function() {
$.ajax({
method: "PUT",
url: "/photos/" + $(this).attr("id"),
success: function(msg_from_server) {
alert(msg_from_server); //"Thanks for voting!" or "Sorry, you already voted!"
}
});
});
如果您有一条声明为resources :photos
的路由,则类似/photos/12
的URL会将请求发送到photos#update
,然后在update action params[:id]
将为12。请注意,您需要添加构造html时html的资源ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.