为什么心不是我的控制器从视图被称为(Why isnt my controller being cal

2019-10-30 05:37发布

我试着打电话从我的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

也Theres一些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;
 }

Answer 1:

试着改变你的视图:

<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>


Answer 2:

这个工作对我很多的试验和错误后:

<%= button_to 'Vote', request_upvote_path(request_id: request.id), remote: true, method: :post, onclick: "upvote(#{request.id})" %>



文章来源: Why isnt my controller being called from the view