我正在尝试从我的erb html调用一个方法,该方法将在控制器中执行操作,但是未执行该操作。
这是html中的代码
<td style="text-align: center">
<%= button_to 'Vote', request_upvote_path(request.id), remote: true, method: :post, onclick: "upvote(#{request.id})", class: 'upvote', id:"voteButton#{request.id}" %>
</td> </tr>
这是控制器中的代码
def upvote
puts 'upvote'
@request = Request.find(params[:request_id])
@request.increment!(:voteCount)
@request.save
render json: { voteCount: request.voteCount }
end
这是路线文件
Rails.application.routes.draw do
resources :requests do
post 'upvote', to: 'requests#upvote'
#get "requests/index"
end
end
那里也有一些JS功能,但是工作正常。
这是JS
$("#ClickMe").attr("disabled", "disabled");
function upvote(id) {
var count = document.getElementById("voteCount" + id).innerHTML;
count = parseInt(count);
count = count + 1;
count = count.toString();
document.getElementById("voteCount" + id).innerHTML = count;
document.getElementById("voteButton" + id).disabled = true;
}
答案 0 :(得分:0)
尝试将视图更改为:
<td style="text-align: center"><%= button_to 'Vote', request_upvote_path(request_id: request.id), remote: true, method: :post, onclick: "upvote(#{request.id})", class: 'upvote', id:"voteButton#{request.id}" %></td> </tr>
答案 1 :(得分:0)
经过大量的反复试验,这对我有用:
<%= button_to 'Vote', request_upvote_path(request_id: request.id), remote: true, method: :post, onclick: "upvote(#{request.id})" %>