为什么我需要在controller中再次指定permit?

Why I need to specify permit again in the controller?

我正在编写一个应用程序,它有一个评论控制器,在控制器内部, 它有像

这样的强参数
def comment_params
  params.require(:comment).permit(:text)
end

但是我发现在使用的时候还需要加permit

@comment = @post.comments.build(params[:comment].permit(:text)) 在创建方法中,否则我会收到错误。

我想知道这里的原因。我正在使用 Rails5.

why I need to specific permit again in the controller?

因为您没有使用 comment_params

@post.comments.build(comment_params)

没有什么神奇的,就用你的方法吧

在此代码中您没有使用它:

@post.comments.build(params[:comment].permit(:text))