在 docs ,他们有:
params = ActionController::Parameters.new(person: { name: "Francesco" })
然后使用 params.fetch(:person)
来获取 person,但是你不能只做 params[:person]
吗?获取相同的数据?
最佳答案
我想这取决于你想要的行为。
params = ActionController::Parameters.new
params.fetch(:person)
会引发错误。正如文档中所反射(reflect)的,如果您愿意,可以使用 fetch
指定进一步的行为。
params = ActionController::Parameters.new
params[:person]
将返回 nil
。
关于ruby-on-rails - 为什么必须使用 fetch 获取 Rails 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44375569/