Reputation: 133
I have a voting feature on my app but can't route back to the same page with the vote applied. I know I have to fill in the link_to methods but my understanding of routing/ruby syntax is a bit limited, so I'm not even sure [post, vote] is correct. I feel I'm missing something else as well. Have I even provided enough information? How should I approach this problem? Or better yet, how can I understand routing better from this? Thank you.
Here is the the error I'm getting:
No route matches [GET] "/posts/13/up-vote"
My voting partial:
<% if policy( Vote.new ).create? %>
<div class="vote-arrows pull-left">
<div>
<%= link_to [post, vote],
post_up_vote_path(post),
class: "glyphicon glyphicon-chevron-up #{(current_user.voted(post) && current_user.voted(post).up_vote?) ? 'voted' : '' }" %>
</div>
<div>
<strong><%= post.points %></strong>
</div>
<div>
<%= link_to [post, vote],
post_down_vote_path(post),
class: "glyphicon glyphicon-chevron-down #{(current_user.voted(post) && current_user.voted(post).down_vote?) ? 'voted' : '' }" %>
</div>
</div>
<% end %>
my routes.rb
Bloccit::Application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index]
end
resources :posts, only: [] do
resources :comments, only: [:create, :destroy]
resources :favorites, only: [:create, :destroy]
post '/up-vote' => 'votes#up_vote', as: :up_vote
post '/down-vote' => 'votes#down_vote', as: :down_vote
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
Upvotes: 0
Views: 87
Reputation: 386
Check out the error message: No route matches [GET] "/posts/13/up-vote"
. It's looking for a [GET]
route, but you've defined a [POST]
route in your config/routes.rb
file.
You need to add method: :post
to both of your link_to
helpers in order to trigger a [POST]
request. Here's what it would look like:
<%= link_to [post, vote], post_down_vote_path(post), class: "glyphicon glyphicon-chevron-down #{(current_user.voted(post) && current_user.voted(post).down_vote?) ? 'voted' : '' }", method: :post %>
Hope this helps!
Upvotes: 1