[英]Globally permit a strong param
Is there a way to permit a param for all actions? 有没有办法允许所有操作都使用参数? The app I'm working on has a feature flag system and I'd like feature flags to be overridable via query string parameters (to aid testing).
我正在使用的应用程序具有功能标记系统,我希望可以通过查询字符串参数覆盖功能标记(以帮助测试)。 I've added something like the following to the
application_controller
: 我已经将以下内容添加到
application_controller
:
before_action :apply_feature_flag_overrides
def apply_feature_flag_overrides
overrides = params.permit![:feature_flags]
current_user.feature_flag_overrides = overrides
end
This is working fine for actions that don't make use of params, but when an action does I get an error like: 对于不使用参数的动作,这种方法很好用,但是当一个动作执行时,我得到如下错误:
ActionController::UnpermittedParameters at /upcoming_posts
found unpermitted parameter: :feature_flags
It appears that params.permit
or params.require
is expected to only be called once for any given action. 似乎
params.permit
或params.require
对于任何给定的动作仅被调用一次。 Is that correct? 那是对的吗? Is there a way to globally white list a set of params for a cross cutting concern like this?
有没有办法像这样跨界关注的问题将一组参数全局列入白名单? I could update every call to
params
to also include :feature_flags
but that's going to be error prone and repetitive. 我可以将对
params
每次调用都更新为也包括:feature_flags
但这将易于出错且重复。
Thanks to @tadman's comment about removing the param once used I got this working. 感谢@tadman对删除使用过的参数的评论,使我能够正常工作。 The updated code looks like:
更新后的代码如下:
before_action :apply_feature_flag_overrides
def apply_feature_flag_overrides
overrides = params.to_unsafe_h[:feature_flags]
current_user.feature_flag_overrides = overrides
params.reject! { |key| key == "feature_flags" }
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.