There is like button for comments. You should be able to like a comment on Post's show page(with index of comments for that specific Post) or on comment's show page
This error appears when I visit this URL:
.../posts/1/comments/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]
When I visit to see a list of comments and like buttons:
.../posts/1/
Similar error appears, but with
... #<Comment id: nil, title: nil, content: nil, user_id: nil ...
like button
<%= link_to "Like", like_post_comment_path(@comment), method: :put, :remote => true %>
routes
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
when I run rake routes
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
Change
<%= link_to "Like", like_post_comment_path(@comment), method: :put, :remote => true %>
To
<%= link_to "Like", like_post_comment_path(@comment.post_id, @comment), method: :put, :remote => true %>
As per your route:
like_post_comment PUT /posts/:post_id/comments/:id/like(.:format) comments#upvote
you need both :post_id
and :id
in the route.
路由是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 %>
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.