5

I have data I am trying to POST that looks like { foo: [[:bar, 1], [:baz, 0]] }.

How do I permit that using strong parameters? The closest I can get is

params.permit(foo: [[]]) which returns {"foo"=>[]}

4
  • I can't find a reference now, but I have a recollection that foo: [][] will work? Commented Nov 4, 2013 at 23:24
  • params.permit(foo: [][]) *** ArgumentError Exception: wrong number of arguments (0 for 1..2) Commented Nov 4, 2013 at 23:31
  • Don't you want params.permit(foo:[[][]])? Or you might want to try params.permit(food:[bar:[],baz:[]]) if nested. Commented Nov 5, 2013 at 2:38
  • The first one fails with the same error I posted above and the second one returns the same as I posted in the original question. Commented Nov 5, 2013 at 17:43

2 Answers 2

5

Maletor,

it seems to me that the strong parameters can't handle array of array. I did read the code of it in github and it deals with Symbol, String and Hash.

For this case you'll have to handle with your own code.

Sign up to request clarification or add additional context in comments.

Comments

0

Basically:

def permitted_params
  result = params.require(:model).permit(:attributes).to_h # No array of arrays or hashes
  result[:model][:array_of_arrays] = params[:model][:array_of_arrays]
  result
end

One step further, say you have a Model#json and you want to store model.json[:array_of_arrays] = [[]]:

def permitted_params
  result = params.require(:model).permit(:attributes).to_h # No array of arrays or hashes
  result[:json] ||= {}
  result[:json].merge!(array_of_arrays: params[:model][:json][:array_of_arrays])
  result
end

Make sure you have permitted all your un-trusted params before you call to_h, and be careful what you merge in afterwards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.