2015-06-08 97 views
0

我对我的应用程序有投票功能,但无法返回到应用投票的同一页面。我知道我必须填写link_to方法,但我对路由/ ruby​​语法的理解有点有限,所以我甚至不确定[post,vote]是正确的。我觉得我也错过了别的东西。我有没有提供足够的信息?我应该如何处理这个问题?或者更好的是,我怎样才能更好地理解路由?谢谢。Ruby-on-Rails投票部分路由

这里是我得到的错误: No route matches [GET] "/posts/13/up-vote"

我的投票部分:

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

我的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 
+1

粘贴您的routes.rb –

回答

1

检查出错误消息:No route matches [GET] "/posts/13/up-vote"。它正在寻找[GET]路线,但您已在config/routes.rb文件中定义了[POST]路线。

您需要将method: :post添加到您的link_to帮助程序中才能触发[POST]请求。下面是它会是什么样子:

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

希望这有助于!

+0

谢谢你,工作! – Kris