Cameron Bass Cameron Bass - 3 months ago 9
Ruby Question

Changing the params before update - rails

I have a update method that has dynamic incoming parameters in order to update the attributes I'd first like to change the params. This is hard to explain so I'll post some code and try to step through it.

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


The requires key is a dynamic parameter because sometimes it's integration_webhook and other times it's integration_pager_duty. So, I'd like to strip it of its integration type so I can just require integration like so
attrs = params.require(:integration).permit(:filters)
. How can I handle dynamic params when trying to update? Because it's hardcoded I'll get this error if it changes
param is missing or the value is empty: integration_pager_duty

Answer

To just resolve the integraion symbol:

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

But I think the resolution in Dynamic Strong params for require - Rails was more explicit.