我试图在rails上的ruby中使用acts_as_votable gem。但是,当我需要将按钮添加到视图时,我会陷入困境。
我希望将投票系统添加到项目列表中,当用户在Web浏览器中填写表单时,该列表将放入html表格中。
我可以插入'喜欢'每个目的地的每一行按钮'项目。但是,这是否意味着我需要写一个&attach;以及一个' attachLikeHandler'用于处理单击like按钮时发生的情况?
这是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]);
}
};
答案 0 :(得分:1)
但是,这是否意味着我需要写一个&#39; attachLikeHandler&#39;功能 处理单击like按钮时会发生什么?
是:
$(".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
等网址会向photos#update
发送请求,然后更新操作params[:id]
内的请求将为12。请注意,在构造html时,您需要将资源ID添加到html。