我正在尝试使用 Acts 作为可投票的宝石对帖子中的评论实施投票系统。在这个阶段我收到了这个错误
ActionController::UrlGenerationError in Posts#show
其次是 -
No route matches {:action=>"upvote", :controller=>"comments", :id=>nil, :post_id=>#<Comment id: 5, post_id: 3, body: "abc", created_at: "2014-01-12 20:18:00", updated_at: "2014-01-12 20:18:00", user_id: 1>, :format=>nil} missing required keys: [:id].
我对路线很弱。
我的路线.rb
resources :posts do
resources :comments do
member do
put "like", to: "comments#upvote"
put "dislike", to: "comments#downvote"
end
end
end
评论控制器
def upvote
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.liked_by current_user
redirect_to @post
end
def downvote
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.downvote_from current_user
redirect_to @post
end
_comment.html.erb
<%= link_to "Upvote", like_post_comment_path(comment), method: :put %>
<%= link_to "Downvote", dislike_post_comment_path(comment), method: :put %>