2009/7/2 khagimoto <[email protected]>:
>
> Thanks, Colin.
> I tried what you suggested like this (with quotes around the number),
> and it worked.
>
> params[:transport][:transports_transits_attributes][:"0"][:city] ==>
> gets me "Seattle" from the initial example.
>
> The problem I have now is that the numbers ("0" and "1246482280809")
> are auto-assigned, so I don't know how I would access them then.  The
> first one always seem to get "0", but there can be many more
> transports_transits for one transport, and all but the first will get
> a random number assigned.  Is there a way I can access them without
> referring to these numbers?  For example, I can get the length of the
> array like this:
>
> len = params[:transport][:transports_transits_attributes].length
>
> and I would love to be able to iterate through the array like this
> (sorry for the bad syntax, but you get the idea..):
>
> for (int i=0; i<len; i++)
>  params[:transport][:transports_transits_attributes](i)[:city]
>
> but I can't seem to do that.  Any suggestions?

It should be [i] not (i) but a much more railsy way is

params[:transport][:transports_transits_attributes].each do |attr|
  # do something with attr[:city]
end

However, I have to say that you seem to be doing rather unusual things
which, when I find myself in such a situation, usually means I am
tackling something in the wrong way.  Can I ask why are you parsing
the params in the first place rather than using them to build objects
in the usual way?

Colin

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to