首页 > 解决方案 > 为什么评论以灰色块呈现,里面没有文字?

问题描述

我使用这个 youtube 教程为我的名为 Rants with polymorphic model 的帖子创建了一个评论功能: Comments with Polymorphic Associations

我在 中看到了评论rails c,但我只看到网站上显示灰色的空评论块。

这是控制台:

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是这样的:

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

灰色块是用 设计的class = "well",它应该在灰色块内有一个注释。

评论区图片

如果你能告诉我应该在哪里检查,那将对我有很大帮助。

编辑:

这可能是另一个问题,但我的评论也没有显示在此处的表格中。 未显示评论的表格图像

app/views/rants/index.html.erb是这样的:

<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是这样的:

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

谢谢你。

标签: ruby-on-railscommentspolymorphic-associations

解决方案


<%=如果您希望它实际呈现在您的页面上,请使用标记:

<%= comment.reply %>

推荐阅读