我是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
我的评论_控制器:
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>
我提交新评论后。关于这个错误的任何帮助都是赞赏或与我的主要目标有关,我已经在这周围盘旋了很长时间。谢谢!
答案 0 :(得分:0)
这里的问题是,每次部分尝试呈现注释时,都会初始化新注释(对于正在呈现的注释)。
查看以下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? %>