Rails 4 strong params formatting with multiple keys

205 views Asked by At

I working a service that takes in any number of custom attributes and serializes it into a hash.

So it would look something like this:

 custom_contacts: {"address_book"=>
   [{"contact_list"=>"user_data",
     "contacts"=>[{"name"=>"user_data", "number"=>"user_data"},
                          {"name"=>"user_data", "number"=>"user_data"},
                          {"name"=>"user_data", "number"=>"user_data"}]}]}

The issue is that I can't quite seem to get this to play nicely with strong params in rails. I've read the documentation here and can't seem to wrap my head around how I would set this up.

1

There are 1 answers

0
rxing On

You may need one "permit" like blow.

a = ActionController::Parameters.new(
    {"data_key"=>
       [{"name_key"=>"user_data",
         "organization_key"=>[{"key1"=>"user_data", "key2"=>"user_data"},
                              {"key3"=>"user_data", "key4"=>"user_data"},
                              {"key5"=>"user_data", "key6"=>"user_data"}]}]}
    )

    a.permit(data_key: [:name_key, organization_key: [:key1, :key2, :key3, :key4, :key5, :key6]])