Permitting array of arrays with strong parameters in rails

4.1k Views Asked by At

I have searched everywhere but does anyone know if it is possible to permit and array of arrays using strong parameters in rails? My code looks like this:

params.require(:resource).permit(:foo, :bar => [[:baz, :bend]])

This is giving me:

ArgumentError (wrong number of arguments (0 for 1..2))

I have also tried:

params.require(:resource).permit(:foo, :bar => [[]])
params.require(:resource).permit(:foo, :bar => [][])
params.require(:resource).permit(:foo, :bar => [])

But these all give me invalid parameter errors or do not process the parameters.

Thanks in advance for any help

2

There are 2 best solutions below

1
On BEST ANSWER

Looking at the code I think this is not possible. you have to flatten the second level.

 def permit(*filters)
  params = self.class.new

  filters.each do |filter|
    case filter
    when Symbol, String
      permitted_scalar_filter(params, filter)
    when Hash then
      hash_filter(params, filter)
    end
  end

  unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters

  params.permit!
end
1
On

Here's an example taken from rails strong parameter Github page:

params.permit(:name, {:emails => []}, :friends => [ :name, { :family => [ :name ], :hobbies => [] }])