本文介绍了为什么不从视图中调用我的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从我的erb html调用一个方法,该方法将在控制器中执行操作,但是未执行该操作。
Im trying to call a method from my erb html that will perform an action in the controller, however the action isn't being performed.
这是其中的代码html
Here's the code in the 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功能,但是工作正常。
Theres also some JS functionality in there, but that's working fine.
这里是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;
}
推荐答案
这对很多人都有用反复试验的结果:
This worked for me after lots of trial and error:
<%= button_to'Vote',request_upvote_path(request_id:request.id),remote:true,方法: :post,onclick: upvote(#{request.id})%>
这篇关于为什么不从视图中调用我的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!