Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 Rails 4.0.4强参数和设置模型?_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 Rails 4.0.4强参数和设置模型?

Ruby on rails 4 Rails 4.0.4强参数和设置模型?,ruby-on-rails-4,Ruby On Rails 4,我刚刚生成了一个新的rails应用程序,并在我的控制器中注意到了这一点 private # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through.

我刚刚生成了一个新的rails应用程序,并在我的控制器中注意到了这一点

private
# Use callbacks to share common setup or constraints between actions.
def set_post
  @post = Post.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def post_params
  params[:post]
end
注意,set_post允许post.find在所有操作中都可以访问。现在还有强参数,它们是后参数

有人能进一步解释一下吗?新的布景怎么样

必须在PostsController顶部定义before_操作

操作之前:设置发布,仅:[:显示,:编辑,:更新,:销毁]

这意味着在任何显示、编辑、更新、销毁操作之前,都会调用set_post方法来设置正在执行操作的post记录

这只是为了避免在每个操作中查找记录的冗余调用