CommentsController未能发表评论

时间:2014-06-21 11:12:41

标签: ruby-on-rails ruby controller comments

我正在尝试使用Ruby on Rails编写一个博客,我到目前为止编写了我的评论控制器,我在Ruby中有点生疏,所以这实际上可能是问题(使用旧的编码方式?)。 / p>

def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.build(params.permit[:comment])
  @comment.save

  redirect_to(:back)
end

现在,它实际上运行正常,但它只是不会发表评论,它只是说"发布xyz分钟前"但实际发布的帖子在发送到网站后不会显示在页面上。如果这是数据库问题或代码失败(即使它的代码失败,我现在也不知道了)!)

如果您需要有关我的代码的更多信息,只需询问:)!

1 个答案:

答案 0 :(得分:0)

您的permit来电中有拼写错误,应该阅读

params.permit(comment: :text)

这假设您的评论模型将实际评论存储在body列中,而您的params看起来像这样:

{ post_id: 42, comment: { text: "..." } }

此外,您应该使用ActiveRecord's validations并捕获这样的无效评论:

def create
  # ...
  if @comment.save
    redirect_to @post
  else
    render :new
  end
end