I have a voting system. When a user clicks “Vote” button, the vote value is incremented by one.
But the problem is, this increment action requires redirecting which I do not want to do.
I’m using will_paginate to paginate my records.
For e.g., if a user voted for an entity on page 2 of the paginated list, it should stay on page 2. But, currently, it redirects to page 1.
In view I have:
<ol>
<% for number in @numbers%>
<li>
<%=number.value%>
<%=button_to '+1',:action=>:increment,:id=>number.id,:remote=>true%>
</li>
<%end%>
</ol>
<%=will_paginate @numbers%>
In the controller I have:
def increment
number=Digit.find(params[:id])
number.increment!(:value)
end
But when I don’t redirect, I get this error:
Missing template digits/increment with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "c:/Sites/tester/app/views"
With your current solution, rails is looking for a view based on the method controller/method_name .
You need to insert a redirect_to after incrementing the number.
To redirect to the previous page, this is one solution: