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.
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>
Here's the code in the controller
def upvote
puts 'upvote'
@request = Request.find(params[:request_id])
@request.increment!(:voteCount)
@request.save
render json: { voteCount: request.voteCount }
end
Here's the routes file
Rails.application.routes.draw do
resources :requests do
post 'upvote', to: 'requests#upvote'
#get "requests/index"
end
end
Theres also some JS functionality in there, but that's working fine.
Here's the 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, method: :post, onclick: "upvote(#{request.id})" %>
Try changing your view to: