Trancey
Trancey

Reputation: 709

Access params hashed with integer - rails

This is directly in reference to this question I had asked here My parameters are generated as,

Parameters: {"utf8"=>"✓", "authenticity_token"=>"temp", "performance_areas"=>{"0"=>{id: 1, description: "This is a test","performance_area"=>"New item"}, "1"=>{id: 2, description: "This is a test2","performance_area"=>"Delete Item"}, "commit"=>"Submit"}

I am trying to access the [:id] in the controller where I iterate using:

params[:performance_areas].each_with_index do |performance_area_parameters,i|
puts performance_area_parameters[i][:id]
end

Gives me errors. I tried a host of other methods trying to convert i to string and some others as well, could not succeed. Is it possible to access id the way I am doing it?

Upvotes: 0

Views: 32

Answers (2)

Abdullah Aden
Abdullah Aden

Reputation: 932

also another solustion could be

params[:performance_areas].each |value| do
  value.each|i,p| do
    puts i + p
  end
end

Upvotes: 0

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

You can do this way

params[:performance_areas].values.each_with_index do |value,i|
  puts value["id"]
  puts value["description"]
  puts value["performance_area"]
end

Upvotes: 1

Related Questions