在Rails中使用集合呈现分部时出现问题



在rails中呈现此部分集合时出现问题。没有错误消息,它只是不会显示在显示视图中。但是,如果我手动循环浏览@page_forum.comments集合(下面的示例(,它就会起作用。我希望部分工作,因为我正在计划使用AJAX功能。就上下文而言,我只想让论坛主题(page_forum(显示其相关评论。

欢迎提出任何建议!

部分评论:

app>views>comments/_comment.html.erb
<h3><%= comment.title %></h3>
<p><%=comment.body%></p>
<p><%=comment.user.username%></p>

应用程序>视图>page_forums>show.html.erb

这项工作:

<% @page_forum.comments.each do |c| %>
<%= c.body%>
<% end %>

这不起作用(但我希望它起作用(:

<% render partial: 'comments/comment', collection: @page_forum.comments %>

评论模型:

class Comment < ApplicationRecord
belongs_to :user
belongs_to :page_forum
end

PageForum模型:

class PageForum < ApplicationRecord
has_one :page
belongs_to :user
has_many :comments
end

此外,服务器日志显示该集合已呈现:

Rendered collection of comments/_comment.html.erb [1 times] (Duration: 3.7ms | Allocations: 908)

这应该可以工作。您错过了"显示"代码结果的=

<%= render partial: 'comments/comment', collection: @page_forum.comments %>

最新更新