在Rails 4中迭代@ post.com之后的额外销毁链接

时间:2022-01-26 19:09:32

I'm trying to render comments under a blog post. Simple authentication makes sure only I can see the destroy links.

我正试图在博客文章中发表评论。简单的身份验证确保只有我可以看到destroy链接。

The issue is this: an additional destroy link gets rendered even when there are zero comments, that redirects to posts/:post_id/comments, which doesn't exist. Only the :create and :destroy resources exist.

问题是:即使没有注释,也会呈现额外的销毁链接,重定向到帖子/:post_id / comments,这不存在。仅存在:create和:destroy资源。

The destroy links of the individual comments get rendered correctly and behave correctly, i.e. send delete request to posts/:post_id/:id.

单个注释的销毁链接正确呈现并且行为正确,即向帖子/:post_id /:id发送删除请求。

My code:

<!-- views/posts/show.html.erb -->

<% provide(:title, @post.title) %>

<%= render "show_listing", :post => @post %>

<% if admin? %>
  <p><%= link_to "Edit", edit_post_path(@post) %></p>
  <p><%= link_to "Destroy", post_path(@post), :confirm => "Zeker weten?", :method => :delete %></p>
<% end %>

<p><%= link_to "Terug naar blog", blog_path %></p>

<h2>Laat een reactie achter:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <p>
    <%= f.label :author, "Naam" %><br>
    <%= f.text_field :author %>
  </p>
  <p>
    <%= f.label :body, "Bericht" %><br>
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit "Verzend" %>
  </p>
<% end %>

<h2>Reacties</h2>
<% @post.comments.each do |comment| %>
  <p><%= comment.author %></p>
  <p><%= comment.body %></p>
  <% if admin? %>
    <p>
      <%= link_to 'Destroy Comment', post_comment_path(@post, comment),
               method: :delete,
               confirm: 'Are you sure?' %>
    </p>
  <% end %>
<% end %>

This is the html being produced. The code inside the .each block gets executed, even when there are zero comments.

这是正在制作的html。即使没有注释,也会执行.each块中的代码。

<p></p>
<p></p>
  <p>
    <a data-confirm="Are you sure?" data-method="delete" href="/posts/16/comments/" rel="nofollow">Destroy Comment</a>
  </p>

Update: this is very weird. When I try in rails console:

更新:这很奇怪。当我在rails控制台中尝试时:

post = Post.find(16)
post.comments.empty? # => true

But when enclosing my block in an unless statement:

但是在我的块中包含一个除非声明:

<% unless @post.comments.empty? %>
  ...
<% end %>

The code still gets executed!! Change to if @post.comments.empty? and it doesn't show anything. (no destroy link).

代码仍然执行!!更改为if @ post.comments.empty?它没有显示任何东西。 (没有销毁链接)。

Update: after rake db:reset and server reset, no changes. Putting

更新:在rake db:reset和server reset之后,没有更改。把

<%= debug comment %>

inside the block returns this:

在块内返回:

--- !ruby/object:Comment
attributes:
  id: 
  author: 
  body: 
  post_id: 1
  created_at: 
  updated_at: 

So there is one phantom comment associated with each post. How did this get here?

因此,每个帖子都有一个幻影评论。这怎么来的?

1 个解决方案

#1


1  

The problem is with your form declaring @post.comments.build. That's making a new (but not yet saved) comment on @post, so that when it comes to your loop, the test @post.comments.empty? does pass because you just made a new comment for the collection.

问题在于您的表单声明@ post.comments.build。那是在@post上发表一个新的(但尚未保存的)评论,所以当涉及你的循环时,测试@ post.comments.empty?确实通过,因为你刚刚为集合做了一个新的评论。

Also maybe just a style thing, but I find the behavior of .blank? much more appealing than .empty?

也许只是一个风格的东西,但我发现.blank的行为?比.empty更有吸引力?

#1


1  

The problem is with your form declaring @post.comments.build. That's making a new (but not yet saved) comment on @post, so that when it comes to your loop, the test @post.comments.empty? does pass because you just made a new comment for the collection.

问题在于您的表单声明@ post.comments.build。那是在@post上发表一个新的(但尚未保存的)评论,所以当涉及你的循环时,测试@ post.comments.empty?确实通过,因为你刚刚为集合做了一个新的评论。

Also maybe just a style thing, but I find the behavior of .blank? much more appealing than .empty?

也许只是一个风格的东西,但我发现.blank的行为?比.empty更有吸引力?