Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么这些评论没有出现在RubyonRails中?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 为什么这些评论没有出现在RubyonRails中?

Ruby on rails 为什么这些评论没有出现在RubyonRails中?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我制作了一个RubyonRails博客作为我项目的一部分。我想给我的博客添加评论。然而,该站点成功地接收并存储了评论,但是当涉及到显示评论时,它就不起作用了。我检查了评论是否是使用rails控制台注册的,是否已注册。它的github存储库是: 注意:如果需要特定代码,请在注释中告诉我 我认为应该是: <%= div_for @comments do |comment| %> 我有点惊讶它没有在这里抛出任何错误 编辑:您也不能在控制器的任何位置设置@comments实例变量:您应

我制作了一个RubyonRails博客作为我项目的一部分。我想给我的博客添加评论。然而,该站点成功地接收并存储了评论,但是当涉及到显示评论时,它就不起作用了。我检查了评论是否是使用rails控制台注册的,是否已注册。它的github存储库是:

注意:如果需要特定代码,请在注释中告诉我

我认为应该是:

<%= 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