控制器未收到来自 Upvote link 的 comment.id
Controller not receiving comment.id from Upvote link
我正在尝试将 Upvote 功能与 acts_as_votable
gem 结合使用。我遇到的问题是,当我将鼠标悬停在 Upvote link 上时,我可以看到 comment.id
没有提供给 URL 预览,然后当我单击 Upvote 错误消息告诉我:
Couldn't find Comment with 'id'=#
<Comment::ActiveRecord_Relation:0x007f813b169658>
我的视图中的评论循环如下所示:
<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>
还有我的 CommentsController:
def upvote
@comment = Comment.find(params[:id])
@comment.upvote_by current_user
redirect_to comments_path
end
还有我的config/routes:
resources :comments do
member do
put "like", to: "comments#upvote"
put "dislike", to: "comments#downvote"
end
end
这感觉应该是很简单的事情,但我就是想不通为什么没有给出id,所以非常感谢帮助:-)
我假设您的@comment 是评论集。如果是这样,那么您应该将 @comment 变量重命名为 @comments 并像这样在您的视图中遍历 @comments 集合:
<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>
如您所见,您可以创建一个 link_to 更新您的评论项,如下所示:
<%= link_to "upvote", like_comment_path(comment), method: :put
我正在尝试将 Upvote 功能与 acts_as_votable
gem 结合使用。我遇到的问题是,当我将鼠标悬停在 Upvote link 上时,我可以看到 comment.id
没有提供给 URL 预览,然后当我单击 Upvote 错误消息告诉我:
Couldn't find Comment with 'id'=#
<Comment::ActiveRecord_Relation:0x007f813b169658>
我的视图中的评论循环如下所示:
<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>
还有我的 CommentsController:
def upvote
@comment = Comment.find(params[:id])
@comment.upvote_by current_user
redirect_to comments_path
end
还有我的config/routes:
resources :comments do
member do
put "like", to: "comments#upvote"
put "dislike", to: "comments#downvote"
end
end
这感觉应该是很简单的事情,但我就是想不通为什么没有给出id,所以非常感谢帮助:-)
我假设您的@comment 是评论集。如果是这样,那么您应该将 @comment 变量重命名为 @comments 并像这样在您的视图中遍历 @comments 集合:
<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>
如您所见,您可以创建一个 link_to 更新您的评论项,如下所示:
<%= link_to "upvote", like_comment_path(comment), method: :put