Ruby on rails 4 Rails 4.0.4强参数和设置模型?
我刚刚生成了一个新的rails应用程序,并在我的控制器中注意到了这一点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.
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记录
这只是为了避免在每个操作中查找记录的冗余调用