Reputation: 1872
I have a links controller and I have defined two custom actions called upvote and downvote. The action definitions:-
def upvote
@link = Link.find_by(id: params[:id])
@link.vote_count += 1
flash[:message]="Vote successfully registered"
redirect_to root_path
end
def downvote
@link = Link.find_by(id: params[:id])
@link.vote_count -= 1
flash[:message]="Vote successfully registered"
redirect_to root_path
end
I have added the routes for these actions in the routes.rb file:-
post '/links/:id', to: 'links#upvote', as: 'upvote'
post '/links/:id', to: 'links#downvote', as: 'downvote'
rails routes shows:-
upvote POST /links/:id(.:format) links#upvote
downvote POST /links/:id(.:format) links#downvote
I'm calling the actions from the view as below:-
<% if link.user != current_user %>
<span class="vote-buttons">
<div class="btn-group-vertical" role="group" aria-label="vote-buttons">
<%= button_to 'Up', 'upvote_path', type:"button", class:"btn btn-xs btn-success" %>
<%= button_to 'Down', 'downvote_path', type:"button", class:"btn btn-xs btn-danger" %>
</div>
</span>
<% end %>
But I keep getting this error:-
No route matches [POST] "/upvote_path"
Why am I seeing this even though rails routes shows that the route exists?
I tried restarting the server too.
Upvotes: 0
Views: 39
Reputation: 33542
No route matches [POST] "/upvote_path"
As per your routes, you are missing an :id
which should be sent inside the route.
<%= button_to 'Up', upvote_path(link), type:"button", class:"btn btn-xs btn-success" %>
Same applies for downvote
<%= button_to 'Down', downvote_path(link), type:"button", class:"btn btn-xs btn-danger" %>
Addition to the above problem, you are not saving @link
after a upvote or a downvote
def upvote
@link = Link.find_by(id: params[:id])
@link.vote_count += 1
@link.save!
flash[:message]="Vote successfully registered"
redirect_to root_path
end
def downvote
@link = Link.find_by(id: params[:id])
@link.vote_count -= 1
@link.save!
flash[:message]="Vote successfully registered"
redirect_to root_path
end
Upvotes: 1
Reputation: 230521
<%= button_to 'Up', 'upvote_path', ...
The URL for your button here is literally "upvote_path"
. Naturally, this url doesn't exist. I think you meant something like this:
<%= button_to 'Up', upvote_path(link), ...
Upvotes: 1