ruby on rails - action controller strong params with value that can be either scalar OR array? -
i have endpoint receives data in such way:
{:stuff => [ { :name => 'item_1', :value => [ { :arcade_game => "dragon's lair", :manufacturer => "cinematronics", } ] }, { :name => 'item_2', :value => 'something else' } ] }
because there values scalar, , arrays, becomes problematic because:
params.permit(:stuff => [:name, :value]).require(:stuff)
will give me:
=> [{"name"=>"item_1"}, {"name"=>"item_2", "value"=>"something else"}]
and
params.permit(:stuff => [:name, :value => [:manufacturer, :arcade_game]]).require(:stuff)
will give me:
=> [{"name"=>"item_1", "value"=>[{"manufacturer"=>"cinematronics", "arcade_game"=>"dragon's lair"}]}, {"name"=>"item_2", "value"=>nil}]
...
so way can figure out around problem looks , feels beyond extremely terrible:
params.permit(:stuff => [:name, :value]).tap |whitelisted| params[:stuff].each_with_index |stuff, index| whitelisted[:stuff][index][:value] = params[:stuff][index].permit(:name, :value => [:arcade_game, :manufacturer]).require(:value) if params[:stuff][index][:value].is_a?(array) end end}.require(:stuff)
ewwwwwwwwwwwwwwwwwwwwwwwwwwwwww!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
how in world supposed handle dynamic values this???
try following. think allow both scalar , array type of values.
params.permit(:stuff => []).require(:stuff)
Comments
Post a Comment