Ruby on Rails错误' nil'不是与ActiveModel兼容的对象。它必须实现:to_partial_path

时间:2017-02-02 15:36:42

标签: ruby-on-rails

我试图为一个简单的博客创建一个评论部分,但是当我尝试渲染它时,我不断收到上述错误。

<div class="panel panel-default">
        <div class="panel-heading">
                Comments
        </div>
        <div class="panel-body">
                <%= render @comment %>
        </div>
</div>

错误位于&lt;%= render @comment%&gt;线。 @comment是零或未定义。

来自我的评论控制员:

  before_action :set_comment, only: [:edit, :update, :destroy]

  def edit
  end

  def create
    @comment = Comment.new(comment_params)

    respond_to do |format|
        if @comment.save
            format.html {redirect_to blog_post_path(id: @comment.blog_post_id), notice: "Comment was created successfully."}
        else
            format.html {redirect_to blog_post_path(id: @comment.blog_post_id), notice: "Comment failed to post."}
          end
    end
  end

  private

  def comment_params
    params.require(:comment).permit(:author, :comment_entry, :blog_post_id)
  end

  def set_comment
    @comment = Comment.find(params[:id])
  end

我无法弄清楚为什么认为@comment设置为nil。任何帮助将不胜感激。

0 个答案:

没有答案