Rails has_many错误-找不到ID [英] Rails has_many error - Couldn't find ID
问题描述
我有评论和回复.评论has_many回复
I have a Comment and Reply. comment has_many replies
routes.rb
routes.rb
resources :comments do
resources :replies
end
当我打开评论/索引然后选择评论/显示时,我会打印与该评论相关的所有回复的信息.
When I open comments/index and then select comments/show I print information about all the replies that are related to that comment.
<% @comment.replies.each do |reply| %>
我还可以创建另一个回复.所有这些都正常工作.
I can also create another reply. All this works properly.
当我决定编辑这些答复之一时,问题就来了.
The problem comes when I decide to edit one of those replies.
<%= link_to 'Edit', edit_comment_reply_path(@comment.replies,reply) %>
奇怪的是,如果注释的ID与我要修改的回复的ID相同,则编辑工作正常.我要修改的特定回复甚至没有必要属于该评论.但是,如果我想编辑ID为例如66的回复,但没有ID为66的评论,则会收到错误消息:
The strange thing is that if there is a comment with an ID the same as the ID of the reply that I want to modify, the edit is working properly. It is not even necessary the particular reply that I want to modify to belongs to that comment. But in case I want to edit reply with ID for example 66, but there is not a comment with id 66, I get an error:
Couldn't find Comment with 'id'=65
<%=form_with(model: @reply, url: [Comment.find(params[:id]), @reply]) do |form| %>
这是replies/_form.html.erb,显然也可以用作编辑表单.在这种情况下,reply.id为65,并且它正在查找具有相同ID的注释(不存在).
This is replies/_form.html.erb which apparently works as an edit form as well. In this case the reply.id is 65 and it is looking for a comment with the same ID which is not present.
推荐答案
您尝试过吗?
<%= link_to 'Edit', edit_comment_reply_path(reply) %>
这篇关于Rails has_many错误-找不到ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!