Ruby on rails 为什么这些评论没有出现在RubyonRails中?
我制作了一个RubyonRails博客作为我项目的一部分。我想给我的博客添加评论。然而,该站点成功地接收并存储了评论,但是当涉及到显示评论时,它就不起作用了。我检查了评论是否是使用rails控制台注册的,是否已注册。它的github存储库是: 注意:如果需要特定代码,请在注释中告诉我 我认为应该是:Ruby on rails 为什么这些评论没有出现在RubyonRails中?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我制作了一个RubyonRails博客作为我项目的一部分。我想给我的博客添加评论。然而,该站点成功地接收并存储了评论,但是当涉及到显示评论时,它就不起作用了。我检查了评论是否是使用rails控制台注册的,是否已注册。它的github存储库是: 注意:如果需要特定代码,请在注释中告诉我 我认为应该是: <%= div_for @comments do |comment| %> 我有点惊讶它没有在这里抛出任何错误 编辑:您也不能在控制器的任何位置设置@comments实例变量:您应
<%= div_for @comments do |comment| %>
我有点惊讶它没有在这里抛出任何错误
编辑:您也不能在控制器的任何位置设置
@comments
实例变量:您应该这样做,或者使用@post.comments
。在_comments.html.erb中
<%= div_for @comments do |comment| %>
<p>
<strong>
Posted <%= time_ago_in_words(comment.created_at) %> ago
</strong>
<br/>
<%= comment.body %>
</p>
<% end %>
检查你的评论
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:name, :body))
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
end
class CommentsController
那么@post.comments的div_会是什么样子呢?原谅我,我是一个新手ror@comments=Post.find(params[:id])。注释正确吗?
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:name, :body))
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
end