简体   繁体   中英

What's causing this route error?

I have a render on a page show view

This direct to the following code

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

But now when I click the upbutton i get the following error:

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

What's going wrong?

My routes are set up like this

  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

And my controller variables are

def show

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

end

Notice how you call the link_to

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

The second argument is wrapped within apex, making it a string. That means the route is generated with the 'post_up_vote_path(post)' interpreted as String id.

Assuming the route definition is correct, it should be

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

The same applies to the corresponding down link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM