没有路线匹配{:action =>" upvote",:controller =>"评论",:post_id =>#

时间:2014-04-09 23:09:44

标签: ruby-on-rails ruby-on-rails-4

有评论按钮。你应该能够对帖子的节目页面(对该特定帖子的评论索引)或评论的节目页面进行评论

访问此网址时会出现此错误:

  

... /帖/ 1 /评论/ 1

No route matches {:action=>"upvote", :controller=>"comments", :post_id=>#<Comment id: 1, title: "This is kinda good!", content: "Testing comment", user_id: 1, post_id: 1, created_at: "2014-04-09 21:35:19", updated_at: "2014-04-09 21:35:19">, :id=>nil, :format=>nil} missing required keys: [:id]

当我访问时查看评论列表和按钮:

  

... /帖/ 1 /

出现类似错误,但

... #<Comment id: nil, title: nil, content: nil, user_id: nil ...

喜欢按钮

<%= link_to "Like", like_post_comment_path(@comment), method: :put, :remote => true %>

路由

  resources :posts do
    resources :comments do
      member do
        put "like", to: "comments#upvote"
        put "dislike", to: "comments#downvote"
      end
    end
  end

comments_controller

def upvote
    @post = Post.find(params[:post_id])
    @comment = Comment.find(params[:id])
    @comment.liked_by current_user
    render "update_likes"
  end

当我运行rake路线时

like_post_comment PUT      /posts/:post_id/comments/:id/like(.:format)        comments#upvote                                                                          
dislike_post_comment PUT      /posts/:post_id/comments/:id/dislike(.:format)     comments#downvote

2 个答案:

答案 0 :(得分:1)

更改

<%= link_to "Like", like_post_comment_path(@comment), method: :put, :remote => true %>

<%= link_to "Like", like_post_comment_path(@comment.post_id, @comment), method: :put, :remote => true %>

根据您的路线:

like_post_comment PUT      /posts/:post_id/comments/:id/like(.:format)        comments#upvote 

您需要路线中的:post_id:id

答案 1 :(得分:0)

路线为like_post_comment PUT /posts/:post_id/comments/:id/like(.:format) comments#upvote,这意味着它需要:post_id:id参数,如下所示:

<%= link_to "Like", like_post_comment_path(:id => @comment.id, :post_id => @post.id), method: :put, :remote => true %>