Dynamic Rails的强大参数 [英] Dynamic Strong params for require - Rails

查看:161
本文介绍了Dynamic 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] =已添加过滤器
redirect_to account_integrations_path
else
呈现:过滤器
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

结帐强参数需要多个,如果这没有回答哟您的问题。



修改



更多动态需求:

  def update_params 
[:integration_webhook,,integration_slack] .each do | model |
return params.require(model).permit(:filters)if params.has_key?(model)
end
end


I have an update method right now that will not work for all situations. It is hard coded in the strong params like this params.require(:integration_webhook).permit(:filters) that all fine right now but sometimes it may be integration_webhook and other times it needs to be integration_slack. Basically, is there a way that I don't need to hardcode the require in the strong params? I'll show my code for clarity.

Update Method:

    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

As you can see it's a standard update method. But I need the integration_webhook params to be dynamic. I'm wondering if there is a model method I could call to strip away the integration_webhook part?

解决方案

Not totally sure how dynamic this needs to be, but assuming that we are either getting an integratino_webhook or a 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

Checkout Strong parameters require multiple if this didn't answer your question.

Edit

For more dynamic requiring:

def update_params
  [:integration_webhook, :integration_slack].each do |model|
    return params.require(model).permit(:filters) if params.has_key?(model)
  end
end

这篇关于Dynamic Rails的强大参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆