I'm trying to implement a voting system to the comments in the posts using Acts as votable gem. At this stage I'm getting this error
ActionController::UrlGenerationError in Posts#show
followed by -
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].
I'm pretty weak with routes.
my routes.rb
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 = @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 %>
You should also pass id
of the post in like_post_comment_path
like like_post_comment_path(post, comment)