How to correctly iterate through params

18,972

When you use each on a Hash like this:

params[:contact][:relationships_attributes].each do |rel|

the block argument, rel, is actually a two element Array with the key at position 0 and the value at position 1. Then your block tries to access the Array as though it was a Hash:

rel[:contact_ids]

and that explains your error message: you can't index an Array with a Symbol.

I think you want to do something more like this:

params['contact']['relationships_attributes'].each do |k, v|
  v['contact_ids'] = [ v['contact_ids'] ] unless v['contact_ids'].is_a?(Array)
end

That will give you a structure like this:

{
  :contact => {
    :relationships_attributes => {
      '1330623965418' => {
        :contact_ids => ['4f4e6696be5a2ca2a7000045']
        :title => 'Wife',
        :_destroy => 'false'
      }
      ...
Share:
18,972
Jason Waldrip
Author by

Jason Waldrip

TLDR Tech Leader, Developer, Open Source Aficionado Languages Elixir Javascript/Typescript Crystal Docker Kubernetes Ruby Go

Updated on July 21, 2022

Comments

  • Jason Waldrip
    Jason Waldrip almost 2 years

    How do I loop through params in my controller so that I can change their values?

    This:

    ...
    params[:contact][:relationships_attributes].each do |rel|
      rel[:contact_ids] = [rel[:contact_ids]]
    end
    ...
    

    Returns an Error of:

    # can't convert Symbol into Integer
    

    To provide more context I am using a single select box in place of a multi-select. So the controller is expecting an array but instead its getting a single value.

    Here Are My Params:

    {"utf8"=>"✓",
    "_method"=>"put",
    "authenticity_token"=>"8FrW59t9vzSep+jEGceMdE/b5q69i5hrEp08yZJJk0o=",
    "contact"=>
      {"first_name"=>"John",
       "last_name"=>"Smith",
       "email_addresses_attributes"=>
        {"0"=>
          {"value"=>"[email protected]",
           "type"=>"Home",
           "_destroy"=>"false",
           "id"=>"4f4eea6dbe5a2ccf03000031"
          },
           "1"=>{"value"=>"[email protected]",
           "type"=>"Home",
           "_destroy"=>"false",
           "id"=>"4f4eea7dbe5a2ccf03000035"
          }
        },
      "phone_numbers_attributes"=>
        {"0"=>
          {"value"=>"555-334-5959",
           "type"=>"Home",
           "_destroy"=>"false",
           "id"=>"4f4eea64be5a2ccf0300002c"}
        },
      "addresses_attributes"=>
        {"0"=>
          {"type"=>"Home",
           "street_address"=>"12345 Any Rd",
           "city"=>"Anytown",
           "state"=>"CO",
           "country"=>"United States",
           "_destroy"=>"false",
           "id"=>"4f4eea64be5a2ccf0300002d"
          }
        },
      "relationships_attributes"=>
        {"1330623965418"=>
          {"contact_ids"=>"4f4e6696be5a2ca2a7000045",
           "title"=>"Wife",
           "_destroy"=>"false"
          }
        }
      },
      "commit"=>"Update Contact",
      "id"=>"4f4e6696be5a2ca2a7000045"
    }