首页 > 解决方案 > Ruby on Rails 5 未创建嵌套评论

问题描述

我是 Rails 的初学者,正在尝试创建一个 Reddit 类型的博客页面。目前,我正在处理嵌套评论,以便用户能够回复评论。在我的应用程序中,我有论坛、帖子和评论。创建帖子和论坛效果很好,在我尝试嵌套回复之前也会发布评论。我的代码如下所示(论坛和用户模型/控制器已被省略):

我的评论模型:

class Comment < ApplicationRecord
 belongs_to :commentable, polymorphic: true
 belongs_to :user
 has_many :comments, as: :commentable
end`

我的帖子模型:

class Post < ApplicationRecord
 belongs_to :forum
 belongs_to :user
 has_many :comments, as: :commentable, dependent: :destroy
end

我的posts_controller:

class PostsController < ApplicationController

def index
 @posts = Post.all
end

def show
 @post = find_post
end

def new
 @post = Post.new
end

def edit
 @post = find_post
end

def create
 @forum = Forum.find(params[:forum_id])
 @post = @forum.posts.create(post_params.merge(user_id: current_user.id))
 redirect_to forum_path(@forum)
end

def update
 @post = find_post

 if @post.update(post_params)
  redirect_to forum_path
 else
  render 'show'
 end
end

def destroy
 @post = find_post
 @post.destroy

 redirect_to forum_path
end

private
 def post_params
  params.require(:post).permit(:title, :content)
 end

private
 def find_post
  @forum = Forum.find(params[:forum_id])
  @forum.posts.find(params[:id])
 end
end

我的comments_controller:

class CommentsController < ApplicationController
 before_action :find_commentable

 def index
  @comments = Comment.all
 end

 def new
  @comment = Comment.new
 end

 def create
  @comment = @commentable.comments.create(comment_params.merge(user_id:        current_user.id))
  if @comment.save
   @post = Post.find_by_id(params[:post_id])
   redirect_to forum_post_path(@post.forum_id, @post), notice: 'Your comment was successfully posted!'
  else
   redirect_to forum_post_path(@post.forum_id, @post), notice: 'error :('
  end
 end

 private
  def comment_params
   params.require(:comment).permit(:content)
  end

private
 def find_commentable
  @commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id]
  @commentable = Post.find_by_id(params[:post_id]) if params[:post_id]
 end
end

我的观点/评论/_comment:

<li>
<%= comment.content %>
<small>Submitted <%= time_ago_in_words(comment.created_at) %> ago</small>

<h2>Add a Reply:</h2>
 <%= form_with(model: [ comment, comment.comments.build ], local: true) do |form| %>
  <p>
    <%= form.text_area :content, placeholder: "Add a Reply" %><br/>
    <%= form.submit %>
  </p>
 <% end %>
<ul>
  <%= render partial: 'comments/comment', collection: comment.comments if comment.comments.any? %>
</ul>
<%= link_to 'Back', forum_post_path(@forum, @post) %>

我的观点/帖子/节目:

<small>Submitted <%= time_ago_in_words(@post.created_at) %> ago</small>

<h3>Comments</h3>

<ul>
 <%= render(partial: 'comments/comment', collection: @post.comments) if @post.comments.any?%>
</ul>

<h2>Add a comment:</h2>

<%= form_with(model: [ @post, @post.comments.build ], local: true) do |form| %>
<%= form.text_field :content, placeholder: "Add a comment" %><br/>
<%= form.submit "Add Comment" %>
<% end %>

<%= link_to 'Back', forum_path(@forum) %>
<%= link_to 'All posts', forum_posts_path(@post.forum_id) %>
<%= link_to 'All comments', post_comments_path(@post) %>

</li>

我的路线:

  resources :posts do
   resources :comments
  end

  resources :comments do
   resources :comments
  end

我不断遇到的第一个错误是,每次我在评论中提交view/posts/show评论时都不会创建。现在,views/comments/_comment 解除了错误:

Showing /example/app/views/comments/_comment.html.erb where line #3 raised:

nil can't be converted to a Time value 

<small>Submitted <%= time_ago_in_words(comment.created_at) %> ago</small>

在我提交新评论后。任何有关此错误的帮助表示赞赏或与我的主要目标有关,我已经围绕这个问题盘旋了很长时间。谢谢!

标签: ruby-on-rails

解决方案


这里的问题是每次部分尝试渲染评论时都会初始化新评论(对于正在渲染的评论)。查看以下byebug输出:

(byebug) pp comment.comments
[#<Comment:0x007f35ce11d900
  id: 5,
  content: "DEF",
  commentable_id: 4,
  commentable_type: "Comment",
  user_id: 1,
  created_at: Wed, 30 May 2018 06:53:51 UTC +00:00,
  updated_at: Wed, 30 May 2018 06:53:51 UTC +00:00>,
 #<Comment:0x007f35ce2a3388
  id: nil,
  content: nil,
  commentable_id: 4,
  commentable_type: "Comment",
  user_id: nil,
  created_at: nil,
  updated_at: nil>]

这条线是罪魁祸首:

 <%= form_with(model: [ comment, comment.comments.build ], local: true) do |form| %>

comment.comments.build初始化一个新的空注释,它是您的代码,然后尝试呈现并抛出错误,因为它没有created_at

然后新初始化的注释由代码呈现。

替换app/views/comments/_comment#13为以下内容将解决问题:

  <%= render(partial: 'comments/comment', collection: comment.comments.where.not(content: nil)) if comment.comments.any? %>

推荐阅读