强参数:如何允许散列任意结构的散列

时间:2019-04-09 09:04:02

标签: ruby-on-rails ruby-on-rails-5 strong-parameters

我想允许使用queries参数。但是我只知道它是一个带有嵌套哈希的哈希。我不知道里面有什么钥匙。它可以例如看起来像这样:

Started PATCH "/en/foos/5ca7a1b883c336410fb0923c" for 127.0.0.1 at 2019-04-09 09:13:46 +0200
Processing by FoosController#update as JS
  Parameters: {"queries"=>"{\r\n  \"query1\": {\r\n    \"query\" : {\r\n      \"order_number\" : {\r\n        \"$in\" : [\"1\", \"2\"]\r\n      },\r\n      \"name\" : \"Batman\"\r\n    },\r\n    \"projection\" : {\r\n      \"_id\" : 1,\r\n      \"order_number\" : 1\r\n    },\r\n    \"sort\" : {\r\n      \"order_number\" : 1\r\n    }\r\n  },\r\n  \"query2\" : {\r\n  }\r\n}", "locale"=>"en", "id"=>"5ca7a1b883c336410fb0923c"}
Completed 200 OK in 15ms (Views: 0.6ms)

在继续之前,我需要解析参数:

params[:queries] = JSON.parse(params[:queries])
# =>
# {
#   "query1": {
#     "query": {
#       "order_number": {
#         "$in": ["1", "2"]
#       },
#       "name": "Batman"
#     },
#     "projection": {
#       "_id": 1,
#       "order_number": 1
#     },
#     "sort": {
#       "order_number": 1
#     }
#   },
#   "query2": {
#   }
# }

解析之后,我想允许它,但是params.permit([{queries: {}}])不允许所有嵌套的哈希。

那么我如何简单地允许queries参数包含任何嵌套内容?

0 个答案:

没有答案