继续获取未定义的方法'title',而我没有任何名为title的方法

时间:2016-10-02 18:14:45

标签: ruby-on-rails

我基本上试图复制这里提供的示例(http://edgeguides.rubyonrails.org/getting_started.html),但名称略有变化。每个post可能有多个comment但我在提交评论时遇到了一些麻烦。 Error page when trying to create a post

我能想到的最接近的事情是我对post个对象进行了验证,要求title字段存在。 我已经看到类似问题的解决方案是确保在控制器中为方法设置了正确的私有/公共范围,但情况似乎并非如此。

这是我的评论控制器代码

class CommentsController < ApplicationController
def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(comment_params)
    redirect_to post_path(@post)
end

private
    def comment_params
        params.require(:comment).permit(:commenter, :body)
    end
end

1 个答案:

答案 0 :(得分:0)

啊,我刚刚意识到自己的问题。 原来我把validates放在ApplicationRecord中,然后让comment.rb继承ApplicationRecord。通过将验证重构为post.rb来解决问题。