路由错误Ruby on Rails - 投票系统

时间:2016-02-24 14:31:04

标签: ruby-on-rails ruby

我使用acts_as_votable gem为我的博客评论实施投票系统。

但是,我收到了路由错误:no route matches. missing required keys: [:id]

  #routes.rb
  resources :articles do
     resources :comments do
        member do
            put "like", to: "comments#upvote"
        end
    end
  end

# comments controller
def upvote
    @comment.upvote
    redirect_to :back
end

# comments/show.html.haml
= link_to like_article_comment_path(@comment), method: :put do
   = @comment.get_upvotes.size

1 个答案:

答案 0 :(得分:2)

如果您使用rake routes | grep like(以获取该路线),您将获得:

like_article_comment PUT    /articles/:article_id/comments/:id/like(.:format)  comments#upvote

所以你错过了链接中的第一个参数 - :article_id。应该是:

= link_to like_article_comment_path(@article, @comment), method: :put do
  = @comment.get_upvotes.size

还在@article方法中添加upvote逻辑。