Я сделал функцию комментариев к своим постам под названием Rants с полиморфной моделью с помощью этого урока youtube: Комментарии с полиморфными ассоциациями .
Я видел комментарии в 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
Спасибо.