【发布时间】:2017-09-14 14:05:09
【问题描述】:
在我的 Rails 控制器中使用 强参数,我如何声明允许的参数可以是 String 或 Array?
我的强参数:
class SiteSearchController < ApplicationController
[...abbreviated for brevity...]
private
def search_params
params.fetch(:search, {}).permit(:strings)
end
end
我想将POST 字符串搜索为String 或Array:
要搜索 1 件事:
{
"strings": "search for this"
}
或者,搜索多个事物:
{
"strings": [
"search for this",
"and for this",
"and search for this string too"
]
}
更新:
目的:我正在创建一个 API,我的用户可以在其中“批量”请求(通过 web-hooks 获取响应),或发出一次性请求(立即响应)同一个端点。这个问题只是我要求的一小部分。
下一部分将执行相同的逻辑,我将允许在多个页面上进行搜索,即:
[
{
"page": "/section/look-at-this-page",
"strings": "search for this"
},
{
"page": "/this-page",
"strings": [
"search for this",
"and for this",
"and search for this string too"
]
}
]
OR 在单个页面上:
{
"page": "/section/look-at-this-page",
"strings": "search for this"
}
(这将使我需要强参数允许发送Object 或Array。
这似乎是一个基本的东西,但我没有看到任何东西。
我知道我可以将 strings 参数设为一个数组,然后需要搜索 1 个东西以在数组中只有 1 个值...但我希望这个参数比这更健壮。
【问题讨论】:
标签: ruby-on-rails strong-parameters