I'm trying to use an Upvote function in conjunction with the acts_as_votable
gem. The problem that I'm having with it is that when I hover on the Upvote link I can see that the comment.id
is not given to the URL preview, and then sure enough when I click Upvote the error message tells me:
Couldn't find Comment with 'id'=#
<Comment::ActiveRecord_Relation:0x007f813b169658>
The comments loop in my views looks like this:
<div class="box">
<% @comment.each do |w| %>
<tr>
<td><b><%= w.title %></b></td><br>
<td><%= w.location %></td><br>
<td><%= w.body %></td><br>
<%= link_to "upvote", like_comment_path(@comment), method: :put
%><br>
</tr>
<% end %>
</div>
And my CommentsController:
def upvote
@comment = Comment.find(params[:id])
@comment.upvote_by current_user
redirect_to comments_path
end
And my config/routes:
resources :comments do
member do
put "like", to: "comments#upvote"
put "dislike", to: "comments#downvote"
end
end
This feels like it should be something very simple, but I just can't figure out why the id is not given, so help would be much appreciated :-)
I assume your @comment is a collection on comments. If so then you should rename your @comment variable to @comments and iterate through the @comments collection in your view like this :
<div class="box">
<% @comments.each do |comment| %>
<tr>
<td><b><%= comment.title %></b></td><br>
<td><%= comment.location %></td><br>
<td><%= comment.body %></td><br>
<%= link_to "upvote", like_comment_path(comment), method: :put
%><br>
</tr>
<% end %>
</div>
As you can see you can create a link_to update your comment item like this :
<%= link_to "upvote", like_comment_path(comment), method: :put