如何获取特定控制器操作的允许参数列表
How to get a list of permitted params for a specific controller action
问题的标题几乎描述了我需要做什么。和这个问题基本一样,一直没有得到答案:
Rails 4: get list of permitted attributes (strong parameters) from a controller
与该问题中提供的示例类似,如果存在,它将等同于以下内容:
account_update_permitted_params = AccountController.permitted_params(:update)
由于强参数的性质,您基本上不能这样做。
你在some_resource_attributes
中定义的是为了过滤请求的参数散列。如果查看方法定义,您会看到 params.require(:some_resource).permit..
- 它对 params
对象进行操作,该对象仅在请求期间出现。
所以有这样的方法好像用处不大
如果您真的想以编程方式访问 some_resource_attributes
中的白名单属性,您可以使用:
class ResourceController < ApplicationController
LIST = %i(foo bar baz)
private
def resource_attributes
params.require(:resource).permit(*LIST)
end
end
ResourceController::LIST
#=> [:foo, :bar, :baz]
但我看不出这有什么意义,因为您可以打开控制器的代码并检查它。
问题的标题几乎描述了我需要做什么。和这个问题基本一样,一直没有得到答案:
Rails 4: get list of permitted attributes (strong parameters) from a controller
与该问题中提供的示例类似,如果存在,它将等同于以下内容:
account_update_permitted_params = AccountController.permitted_params(:update)
由于强参数的性质,您基本上不能这样做。
你在some_resource_attributes
中定义的是为了过滤请求的参数散列。如果查看方法定义,您会看到 params.require(:some_resource).permit..
- 它对 params
对象进行操作,该对象仅在请求期间出现。
所以有这样的方法好像用处不大
如果您真的想以编程方式访问 some_resource_attributes
中的白名单属性,您可以使用:
class ResourceController < ApplicationController
LIST = %i(foo bar baz)
private
def resource_attributes
params.require(:resource).permit(*LIST)
end
end
ResourceController::LIST
#=> [:foo, :bar, :baz]
但我看不出这有什么意义,因为您可以打开控制器的代码并检查它。