Reading Hash/Array

Hi all,

My app is generating a form with several bill_items that should be updated (fields to be updated are description and net).

The text_field_tag names are bill_item[update][#{id}][description] and bill_item[update][#{id}][net] (and I added bill_item[update][#{id}] [id] just in case)

The param received is:

{"commit"=>"Save", "authenticity_token"=>"6184e60b9928fadecbdf3713ffcf770a2402c8c2", "bill_item"=>{"update"=>{"1"=>{"id"=>"1",                                                "description"=>"Daily monitoring",                                                "net"=>"1:00"},                                       "2"=>{"id"=>"2",

"description"=>"Development",                                                "net"=>"3:00"}                                      }                     }, "id"=>"1"}

How can I read/process this data?

I am trying to do something like

params[:bill_item][:update].each do |item|   bill_item = BillItem.find(item[id])   bill_item.description = item[description] = item[net] end

but of course the access to the hash is not correct.

I know that item[id] is not correct, thats why I said that the access to the hash is incorrect (I should have been more specific), it was just to give you and idea of what I wanted.

If I try to item["id"] rails tells me "can't convert String into Integer".

And if I try to use item["id"].to_i, the error reported remains the same.

def update_data     unless params[:bill_item].nil? || params[:bill_item][:update].nil?       params[:bill_item][:update].each do |item|         bill_item = BillItem.find(item["id"].to_i)         bill_item.description = item["description"] = item["net"]       end[:notice] = 'Item(s) updated'     else[:warning] = 'No data received'     end   end

Error: can't convert String into Integer

Trace: app/controllers/billing_controller.rb:101:in `' app/controllers/billing_controller.rb:101:in `update_data' app/controllers/billing_controller.rb:100:in `each' app/controllers/billing_controller.rb:100:in `update_data' app/controllers/billing_controller.rb:63:in `update_items' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.1/lib/action_controller/ base.rb:1168:in `send' (...)

Parameters: {"commit"=>"Save", "authenticity_token"=>"6184e60b9928fadecbdf3713ffcf770a2402c8c2", "bill_item"=>{    "update"=>{      "1"=>{        "id"=>"1",        "description"=>"Daily monitoring",        "net"=>"1:00"},      "2"=>{        "id"=>"2",        "description"=>"Development",        "net"=>"3:00"}      } }, "id"=>"1"}

Yes, 101 is the find.

If I try to use item = item.to_hash inside the do block, I get:

undefined method `to_hash' for #<Array:0x364a0e8>

This tells me that it is an array. But why the hell I'm not being able to access its values? not even with item[1], item[2] etc.

How can I add something like debug(item) to the log so that I can see it's structure?

Thanks for all the help Ryan.

Fixed it.

I used item[0], item[1] and item[2] to access the array. But when I did this, I noticed that the description (= item[1]) was receiving: ! map:HashWithIndifferentAccess (...) so actualy item[1] was returning a hash instead of the description. Then I understood how rails was (correctly) interpreting the whole params[:bill_item]. [:update] was an array, with the key being the id of the field, and the value being the hash with the params that I sent (id, description, net)

So this is the correct/working code:

params[:bill_item][:update].each do |key, value|         bill_item = BillItem.find(key.to_i)         bill_item.description = value[:description] = value[:net] end

Again, thanks for your help Ryan!