说明 Rails 表单中 Strong Parameters 这个功能。
环境确认
Rails 的 params 方法
在 Rails 中,可以用控制器内的params
方法,取得 hash 中的 name 属性的值。
-
product[name]
,给 product 加上 [name],就可以作为 hash 接受。
-
product[category_ids][]
,给 product 中加上二维 [],就可以作为数组来接收值。
例如,从以下添加商品信息的表单中接受数据。
<form action="/products" method="post">
<div style="display:none">
<input name="utf8" type="hidden" value="✓">
<input name="authenticity_token" type="hidden" value="gpATAhefgIhzK7cuGsWVuKGZCE4gJeEbrMv+a/wOJ+I=...