Seth
Seth

Reputation: 169

Issue rendering partial with collection in Rails

Having an issue rendering this partial collection in rails. There's no error message, it simply won't display on the show view. However, if I manually loop through the @page_forum.comments collection (example below), it will work. I'm wanting the partials to work since I'm planning on using AJAX functionality. For context, I just want a forum topic (page_forum) to display its associated comments.

Any advice is appreciated!

The comment partial:

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

This works:

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

This does not work (but I want it to):

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

Comment model:

class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :page_forum
end

PageForum model:

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

Also the server logs are saying the collection was rendered:

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

Upvotes: 0

Views: 134

Answers (1)

Rockwell Rice
Rockwell Rice

Reputation: 3002

This should work. You missed the = which "shows" the result of the code.

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

Upvotes: 2

Related Questions