我有一个具有动态传入参数的更新方法,以便更新我首先要更改参数的属性。这很难解释,所以我会发布一些代码并尝试逐步完成它。
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
requires键是一个动态参数,因为有时它是integration_webhook,有时则是integration_pager_duty。所以,我想剥离它的集成类型,所以我只需要像attrs = params.require(:integration).permit(:filters)
这样的集成。在尝试更新时如何处理动态参数?因为它是硬编码的,如果它改变param is missing or the value is empty: integration_pager_duty
答案 0 :(得分:2)
要解析整合符号:
def update
attrs = params.require(integration).permit(:filters)
# ...
end
private
def integration
:integration_webhook if params.has_key?(:integration_webhook)
:integration_slack if params.has_key?(:integration_slack)
end
但我认为Dynamic Strong params for require - Rails中的决议更加明确。