[英]Ruby on Rails 5 Nested comments not being created
I'm a beginner in rails and trying to create a Reddit type blog page. 我是Rails的初学者,正在尝试创建Reddit类型的博客页面。 Currently, I'm working on nested comments so the user is able to reply to comments. 目前,我正在处理嵌套评论,以便用户能够回复评论。 In my app I have forums, posts and comments. 在我的应用中,我有论坛,帖子和评论。 Creating posts and forums has worked perfectly, also posting comments until I tried nesting replies. 创建帖子和论坛效果很好,也可以发表评论,直到我尝试嵌套回复为止。 My code looks like this (Forums and Users models/controllers have been omitted) : 我的代码如下(忽略了论坛和用户模型/控制器):
My Comment model: 我的评论模型:
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
belongs_to :user
has_many :comments, as: :commentable
end`
My Post model: 我的帖子模型:
class Post < ApplicationRecord
belongs_to :forum
belongs_to :user
has_many :comments, as: :commentable, dependent: :destroy
end
My posts_controller: 我的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
My comments_controller: 我的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
My views/comments/_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) %>
My views/post/show: 我的观点/帖子/节目:
<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>
My routes: 我的路线:
resources :posts do
resources :comments
end
resources :comments do
resources :comments
end
The first errors I kept getting were that every time I submitted a comment in view/posts/show
the comment wouldn't create. 我一直遇到的第一个错误是,每当我在view/posts/show
提交评论时,都会创建评论。 Now, views/comments/_comment lifts the error: 现在,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>
after I submit a new comment. 在我提交新评论后。 Any help regarding this error is appreciated or related to my main goal, I have been circling around this for a long time. 关于此错误的任何帮助都将受到赞赏或与我的主要目标有关,我已经绕了很长时间了。 Thanks! 谢谢!
The problem here is that new comment is initialized(for the comment which is being rendered) every time the partial is trying to render the comment. 这里的问题是,每次部分尝试渲染注释时,都会初始化新注释(针对正在渲染的注释)。 Check out the following byebug
output: 查看以下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>]
This line is the culprit: 这是罪魁祸首:
<%= form_with(model: [ comment, comment.comments.build ], local: true) do |form| %>
comment.comments.build
initializes a new empty Comment which is your code then tries to render and throws the error as it does not have created_at
. comment.comments.build
初始化一个新的空Comment,这是您的代码,然后尝试呈现并抛出错误,因为它没有created_at
。
And then the newly initialized comment is being rendered by the code. 然后,代码将呈现新初始化的注释。
Replacing app/views/comments/_comment#13
with the following will resolve the issue: 将app/views/comments/_comment#13
替换为以下内容将解决此问题:
<%= render(partial: 'comments/comment', collection: comment.comments.where.not(content: nil)) if comment.comments.any? %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.