要求的动态强参数 - Rails
Dynamic Strong params for require - Rails
我现在有一个更新方法,它不适用于所有情况。它被硬编码在像这样 params.require(:integration_webhook).permit(:filters)
这样的强参数中,现在一切正常,但有时它可能是 integration_webhook
而其他时候它需要是 integration_slack
。基本上,有没有一种方法我不需要在强参数中对要求进行硬编码?为了清楚起见,我将展示我的代码。
更新方法:
def update
@integration = current_account.integrations.find(params[:id])
attrs = params.require(:integration_webhook).permit(:filters)
if @integration.update_attributes(attrs)
flash[:success] = "Filters added"
redirect_to account_integrations_path
else
render :filters
end
end
如您所见,这是一种标准的更新方法。但我需要 integration_webhook 参数是动态的。我想知道是否有一个模型方法可以调用来去除 integration_webhook 部分?
不完全确定这需要多动态,但假设我们得到 integratino_webhook
或 integration_slack
。
def update
@integration = current_account.integrations.find(params[:id])
if @integration.update_attributes(update_params)
# ...
else
# ...
end
end
private
def update_params
params.require(:integration_webhook).permit(:filters) if params.has_key?(:integration_webhook)
params.require(:integration_slack).permit(:filters) if params.has_key?(:integration_slack)
end
如果这没有回答您的问题,请查看 Strong parameters require multiple。
编辑
更多动态要求:
def update_params
[:integration_webhook, :integration_slack].each do |model|
return params.require(model).permit(:filters) if params.has_key?(model)
end
end
在我的脑海中,这样的事情应该可行。命名约定不是最好的,但它的结构将允许您在需要时添加到列表中。
def update
@integration = current_account.integrations.find(params[:id])
if @integration.update_attributes(webhook_and_slack_params)
flash[:success] = "Filters added"
redirect_to account_integrations_path
else
render :filters
end
end
def webhook_and_slack_params
[:integration_webhook, :integration_slack].each do |the_params|
if(params.has_key?(the_params))
params.require(the_params).permit(:filters)
end
end
我现在有一个更新方法,它不适用于所有情况。它被硬编码在像这样 params.require(:integration_webhook).permit(:filters)
这样的强参数中,现在一切正常,但有时它可能是 integration_webhook
而其他时候它需要是 integration_slack
。基本上,有没有一种方法我不需要在强参数中对要求进行硬编码?为了清楚起见,我将展示我的代码。
更新方法:
def update
@integration = current_account.integrations.find(params[:id])
attrs = params.require(:integration_webhook).permit(:filters)
if @integration.update_attributes(attrs)
flash[:success] = "Filters added"
redirect_to account_integrations_path
else
render :filters
end
end
如您所见,这是一种标准的更新方法。但我需要 integration_webhook 参数是动态的。我想知道是否有一个模型方法可以调用来去除 integration_webhook 部分?
不完全确定这需要多动态,但假设我们得到 integratino_webhook
或 integration_slack
。
def update
@integration = current_account.integrations.find(params[:id])
if @integration.update_attributes(update_params)
# ...
else
# ...
end
end
private
def update_params
params.require(:integration_webhook).permit(:filters) if params.has_key?(:integration_webhook)
params.require(:integration_slack).permit(:filters) if params.has_key?(:integration_slack)
end
如果这没有回答您的问题,请查看 Strong parameters require multiple。
编辑
更多动态要求:
def update_params
[:integration_webhook, :integration_slack].each do |model|
return params.require(model).permit(:filters) if params.has_key?(model)
end
end
在我的脑海中,这样的事情应该可行。命名约定不是最好的,但它的结构将允许您在需要时添加到列表中。
def update
@integration = current_account.integrations.find(params[:id])
if @integration.update_attributes(webhook_and_slack_params)
flash[:success] = "Filters added"
redirect_to account_integrations_path
else
render :filters
end
end
def webhook_and_slack_params
[:integration_webhook, :integration_slack].each do |the_params|
if(params.has_key?(the_params))
params.require(the_params).permit(:filters)
end
end