Rails 4 - 带脚手架的强参数 - params.fetch

Rails 4 - strong parameters with scaffold - params.fetch

我使用脚手架命令在我的 Rails 4 应用程序中制作我的组件。

最近,设置强参数的方法中使用的术语已从 params.require 更改为 params.fetch,现在设置中有花括号。

private
    # Never trust parameters from the scary internet, only allow the white list through.
    def engagement_params
      params.fetch(:engagement, {})
    end

我找不到任何说明更改或如何使用它的文档。

我还能在 fetch 命令中写入 params.fetch(:engagement).permit(:opinion) 吗?我不知道如何处理花括号。

如何使用这种新的表达形式完成强参数?

我从来没有遇到过这种情况,但在这里,我找到了对 fetch 方法的引用

http://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-fetch

Can I still write params.fetch(:engagement).permit(:opinion) into the fetch command?

是的,您仍然可以使用

params.fetch(:engagement).permit(:attributes, :you, :want, :to, :allow)

I don't know what to do with the curly braces.

这是一个默认值,如果键不存在则将返回该值,否则会抛出错误

params.fetch(:engagement)
#=> *** ActionController::ParameterMissing Exception: param is missing or the value is empty: engagement

params.fetch(:engagement, {})
#=> {}

params.fetch(:engagement, 'Francesco')
#=> 'Francesco'

How do I complete the strong params using this new form of expression?

params.fetch(:engagement).permit(:attributes, :you, :want, :to, :allow)