Hello, I have a hash, represented by @hash. I want to display the keys and values of @hash in my view. I realize that if say one of keys was "ip_address" i could simple add this to my view: <%= @hash[''ip_address''] %>. However! @hash changes...Thus I won''t always have the same keys. I need a way to show key and values of a hash without specifically naming each key that I want in my view. Any suggestions would be greatly appreciated. Thanks, Toro --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This is a basic Ruby question. You might want to look into buying a
primer on Ruby and learn the basics; it''ll help you a lot.
<% @hash.each do |key, value| %>
<%=h "#{key.to_s} #{value.to_s}" %>
<% end %>
You can iterate through hashes in many various ways, including sorting them:
<% @hash.keys.sort.each do |key| %>
<%=h "#{key.to_s} #{@hash[key]}" %>
<% end %>
If these are controlled by someone not you (that is, submitted by the
user) using <%=h ... %> ensures that any HTML contained within would
not render directly into the page, but instead convert < into < and
so on. This is a basic security measure.
--Michael
On Sun, Mar 1, 2009 at 8:02 PM, Toro
<torymbrady-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> Hello,
>
> I have a hash, represented by @hash. I want to display the keys and
> values of @hash in my view.
> I realize that if say one of keys was "ip_address" i could simple
add
> this to my view: <%= @hash[''ip_address''] %>.
> However! @hash changes...Thus I won''t always have the same keys. I
> need a way to show key and values of a hash without specifically
> naming each key that I want in my view.
>
> Any suggestions would be greatly appreciated.
>
> Thanks,
>
> Toro
>
> >
>
--
(Ruby, Rails, Random) blog: http://skandragon.blogspot.com/
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Mar 2, 2:02 am, Toro <torymbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > I have a hash, represented by @hash. I want to display the keys and > values of @hash in my view. > I realize that if say one of keys was "ip_address" i could simple add > this to my view: <%= @hash[''ip_address''] %>. > However! @hash changes...Thus I won''t always have the same keys. I > need a way to show key and values of a hash without specifically > naming each key that I want in my view. >Iterate over the hash with each ? Fred> Any suggestions would be greatly appreciated. > > Thanks, > > Toro--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Michael! --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---