Search code examples
ruby-on-railscommentspolymorphic-associations

Why are comments rendered in grey blocks with no words inside?


I made a comment function to my posts called Rants with polymorphic model with this youtube tutorial: Comments with Polymorphic Associations.

I saw the comments in rails c, but I only see grey empty comment blocks displayed on the website.

Here is the console:

2.5.1 :001 > Rant.first.comments
  Rant Load (0.2ms)  SELECT  "rants".* FROM "rants" ORDER BY "rants"."id" ASC LIMIT ?  [["LIMIT", 1]]
  Comment Load (0.2ms)  SELECT  "comments".* FROM "comments" WHERE "comments"."commentable_id" = ? AND "comments"."commentable_type" = ? LIMIT ?  [["commentable_id", "1"], ["commentable_type", "Rant"], ["LIMIT", 11]]
 => #<ActiveRecord::Associations::CollectionProxy [#<Comment id: 1, commentable_type: "Rant", commentable_id: "1", integer: nil, user_id: nil, reply: "Testing!", created_at: "2018-10-18 08:57:16", updated_at: "2018-10-18 08:57:16">, #<Comment id: 3, commentable_type: "Rant", commentable_id: "1", integer: nil, user_id: nil, reply: "yooo\r\n", created_at: "2018-10-18 11:13:55", updated_at: "2018-10-18 11:13:55">, #<Comment id: 4, commentable_type: "Rant", commentable_id: "1", integer: nil, user_id: nil, reply: "wooooooo\r\n", created_at: "2018-10-18 11:14:11", updated_at: "2018-10-18 11:14:11">, #<Comment id: 14, commentable_type: "Rant", commentable_id: "1", integer: nil, user_id: nil, reply: "aa", created_at: "2018-10-18 16:54:03", updated_at: "2018-10-18 16:54:03">]> 
2.5.1 :002 > exit

app/views/comments/_comments.html.erb is like this:

<h3>Comments</h3>
<% commentable.comments.each do |comment| %>
  <div class="well">
   <% comment.reply %>
  </div>
<% end %>

The grey block is designed with class = "well", and it's supposed to have a comment inside the grey block.

Comment section image

If you can tell me where I should check, that would help me a lot.

Edited:

It might be another problem but I don't have my comments shown on the table here as well. table image with no comments shown

app/views/rants/index.html.erb is like this:

<h1>Rants</h1>
<table class="table table-striped">
  <thead>
    <tr>
      <th colspan="2">Title</th>
      <th colspan="2">Gaijintag</th>
      <th class="text-center">Content</th>
      <th class="text-center">Replies</th>
    </tr>
  </thead>
  <tbody>
    <% @rants.each do |rant| %>
      <tr>
        <td colspan="2"><%= rant.title %></td>
        <td colspan="2"><%= rant.gaijintag %></td>
        <td><%= rant.content %></td>
        <td><%= rant.comments %></td>
        <td class="text-right">
          <%= link_to 'Reply', polymorphic_path(rant), :action => :new, :class => 'btn btn-link btn-xs'%>
          <%= link_to 'Edit', edit_rant_path(rant), :class => 'btn btn-link btn-xs' %>
          <%= link_to 'Delete', rant_path(rant), :class => 'btn btn-xs btn-danger',
                          :method => :delete, :data => { :confirm => 'Are you sure?' } %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>
<%= paginate @rants %>
<br>
<%= link_to 'New Rant', new_rant_path, :class => 'btn btn-primary' %>

app/controllers/comments_controller.rb is like this:

class CommentsController < ApplicationController
    before_action :authenticate_user!

    def create
        @comment = @commentable.comments.new comment_params
        @comment.save
        redirect_to @commentable, notice: "Your reply was successfully posted!"
    end

    private

        def comment_params
            params.require(:comment).permit(:reply)
        end

end

Thank you.


Solution

  • Use <%= marker if you want it to be actually rendered on your page:

    <%= comment.reply %>