【问题标题】:Issue rendering partial with collection in Rails在 Rails 中使用集合渲染部分问题
【发布时间】:2020-03-27 16:36:32
【问题描述】:

在 Rails 中呈现此部分集合时出现问题。没有错误消息,它根本不会显示在显示视图上。但是,如果我手动遍历 @page_forum.cmets 集合(下面的示例),它将起作用。我希望部分工作,因为我打算使用 AJAX 功能。对于上下文,我只想要一个论坛主题 (page_forum) 来显示其关联的 cmets。

感谢任何建议!

评论部分:

app>views>comments/_comment.html.erb

    <h3><%= comment.title %></h3>
    <p><%=comment.body%></p>
    <p><%=comment.user.username%></p>

app>views>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)

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    这应该可行。您错过了“显示”代码结果的 =

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

    【讨论】:

      猜你喜欢
      • 2016-04-24
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多