我基本上试图复制这里提供的示例(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
答案 0 :(得分:0)
validates
放在ApplicationRecord中,然后让comment.rb
继承ApplicationRecord。通过将验证重构为post.rb
来解决问题。