导致此路线错误的原因是什么?

时间:2015-01-18 12:35:42

标签: ruby-on-rails ruby

我在页面显示视图上有渲染

   

这直接到以下代码

<div>
 <div><%= link_to " ", 'post_up_vote_path(post)', class: 'glyphicon glyphicon-chevron-up', method: :post %></div>
 <div><strong><%= post.points %></strong></div>
 <div><%= link_to " ", 'post_down_vote_path(post)', class: 'glyphicon glyphicon-chevron-down', method: :post %></div>
 </div>

但现在当我点击向上按钮时,我收到以下错误:

Couldn't find Post with 'id'=post_up_vote_path(post)

出了什么问题?

我的路线设置如下

  resources :post, only: [] do
  resources :comments, only: [:create, :destroy]

 post '/up-vote' => 'votes#up_vote', as: :up_vote
 post '/down-vote' => 'votes#down_vote', as: :down_vote

end

我的控制器变量是

def show

 @post = Post.find(params[:id])
 @topic = Topic.find(params[:topic_id])
 @comments = @post.comments

end

1 个答案:

答案 0 :(得分:3)

请注意您如何调用link_to

<%= link_to " ", 'post_up_vote_path(post)', class: 'glyphicon glyphicon-chevron-up', method: :post %>

第二个参数包含在顶点内,使其成为一个字符串。这意味着生成路线的'post_up_vote_path(post)'被解释为String id。

假设路线定义正确,则应为

<%= link_to " ", post_up_vote_path(post), class: 'glyphicon glyphicon-chevron-up', method: :post %>

这同样适用于相应的下行链接。