I have a Sidekiq worker to wich i send my controller params. My controller params look like this.
def my_params
params.require(:users).permit(employees: [:param1, param_requested_attributes: [:attribute]])
end
So when i send my JSON to the controller and check with byebug, params are correctly formatted. but when i send them to the worker like:
MyWorker.perform_async(my_params)
I iterate over each "employee" as:
my_params.each do |employee|
data = JSON.parse(raw_data.gsub('=>', ':')) # to correctly format my json data
end
and i get an "unexpected token error" because 'params_requested_attributes' looks like:
"params_requested_attributes"=>[<ActionController::Parameters> {"attribute"=>"value"} permitted: true> ]
My question is, how can i avoid this "ActionController::parameters" when trying to JSON.parse my params? It only happens when i try to use these nested_attributes. I just want a raw json, but for some reason i get this "action controller params".
CodePudding user response:
Your problem is that my_params
:
def my_params
params.require(:users).permit(...)
end
gives you an instance of ActionController::Parameters
and the params_requested_attributes
inside that is another instance of ActionController::Parameters
. Then you enqueue a job with:
MyWorker.perform_async(my_params)
which has to serialize my_params
into a database and that serialization isn't handling the inner ActionController::Parameters
the way you want it to.
You could convert the parameters to a nested hash yourself:
MyWorker.perform_async(my_params.to_h)
to get rid of the the ActionController::Parameters
and end up with plain hashes and arrays in your job.
If you really want JSON that your job manually unpacks then you'll have to massage my_params
a little more. Perhaps you'd want to my_params.to_h
and then JSON encode then nested params_requested_attributes
. This sounds like a solution in search of a problem, I think passing my_params.to_h
to the job is where you want to go.
CodePudding user response:
JSON.parse
turns a json string into a ruby object. So if you need just the raw json string you should use raw_data
and skip the .gsub
call.
However I cannot see from your example how raw_data
relates to the employee value.