Hi all, I''ve been reading lots of Ruby and Rails resources around the net and have just been fiddling around with RoR. I can''t seem to figure out the proper way to dump a hash. I realize that I can <%= debug(params) %> or <%= params.inspect %>, but I''d like to instead generate something like: <ul> <li>Somekey: somevalue</li> <li>Otherkey: othervalue</li> </ul> In perl I would: print "<ul>\n"; foreach $key (keys(%params)) { print " <li>$key: $params{$key}</li>\n"; } print "<ul>\n"; I think this is the closest I got (index.rhtml): <ul> <%= params.each {|key,value| puts "#{key}: #{value}" } %> </ul> It puts the nicely formatted list in the webrick log, which makes sense ("puts"). I got this in the browser (strange): <ul> actionindexfoo1controllertestbaricky space </ul> What is the Ruby way? Thanks. Regards, Rich
On 9/28/05, Rich Duzenbury <rduz-6Hh2x1qHOZDQT0dZR+AlfA@public.gmane.org> wrote:> Hi all, > > I''ve been reading lots of Ruby and Rails resources around the net and > have just been fiddling around with RoR. > > I can''t seem to figure out the proper way to dump a hash. I realize > that I can <%= debug(params) %> or <%= params.inspect %>, > > but I''d like to instead generate something like: > > <ul> > <li>Somekey: somevalue</li> > <li>Otherkey: othervalue</li> > </ul> > > In perl I would: > print "<ul>\n"; > foreach $key (keys(%params)) { > print " <li>$key: $params{$key}</li>\n"; > } > print "<ul>\n"; > > > I think this is the closest I got (index.rhtml): > <ul> > <%= params.each {|key,value| puts "#{key}: #{value}" } %> > </ul> > > It puts the nicely formatted list in the webrick log, which makes sense > ("puts"). > > I got this in the browser (strange): > <ul> > actionindexfoo1controllertestbaricky space > </ul> > > What is the Ruby way? > > Thanks. > > Regards, > Rich<ul> <% params.each do |k,v| -%> <li><%= k %>: <%= v %></li> <% end -%> </ul> -- rick http://techno-weenie.net
On Sep 28, 2005, at 7:29 PM, Rich Duzenbury wrote:> <ul> > <%= params.each {|key,value| puts "#{key}: #{value}" } %> > </ul>> <%= params.each {|key,value| _erbout << "#{key}: #{value}" } %>Try that instead. puts doesn''t work in erb to output to the browser, just to stderror. Cheers- -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sep 28, 2005, at 7:29 PM, Rich Duzenbury wrote:> I can''t seem to figure out the proper way to dump a hash. I realize > that I can <%= debug(params) %> or <%= params.inspect %>,Almost! Your rhtml is rendered to a string, not written to stdout, so avoid puts and friends.> I think this is the closest I got (index.rhtml): > <ul> > <%= params.each {|key,value| puts "#{key}: #{value}" } %> > </ul><ul> <% params.each do |key, value| -%> <li><%=h key %>: <%=h value %></li> <% end -%> </ul> (note) The -%> trims the following newline to keep the generated HTML clean. h is a method that html-escapes the parameter. Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDO1RgAQHALep9HFYRAiEcAKCUp8dqom83lrzjP16S43CqIhOpOwCgl0uq Og/mhZFC6K8I9/0omX/8B4I=vp9m -----END PGP SIGNATURE-----
On Wed, 2005-09-28 at 19:41 -0700, Jeremy Kemper wrote:> <ul> > <% params.each do |key, value| -%> > <li><%=h key %>: <%=h value %></li> > <% end -%> > </ul> >Ahh, thanks for that. I suppose this can be achieved with the ''brace'' block form of each on one line of code? I couldn''t make it work. Regards, Rich
On Wed, 2005-09-28 at 19:39 -0700, Ezra Zygmuntowicz wrote:> > > <%= params.each {|key,value| _erbout << "#{key}: #{value}" } %> > > > Try that instead. puts doesn''t work in erb to output to the browser, > just to stderror. >Thanks, Ezra. I hadn''t seen ''_erbout'' mentioned _anywhere_. Course, I''m a noob. Hmm. underscore == internal? Is this a bad idea to use? Potential to change from one release of Rails to another? Thanks. Regards, Rich
On Sep 28, 2005, at 8:46 PM, Rich Duzenbury wrote:> On Wed, 2005-09-28 at 19:39 -0700, Ezra Zygmuntowicz wrote: > >> >> >>> <%= params.each {|key,value| _erbout << "#{key}: #{value}" } %> >>> >>> >> Try that instead. puts doesn''t work in erb to output to the browser, >> just to stderror. >> >> > > Thanks, Ezra. I hadn''t seen ''_erbout'' mentioned _anywhere_. Course, > I''m a noob. > > Hmm. underscore == internal? Is this a bad idea to use? > Potential to > change from one release of Rails to another? > > Thanks. > > Regards, > Rich >Well Rich, it won''t change with updates to rails as it is a part of erb which is in the ruby standard library. But it might not be the best answer here. _erbout represents the erb buffer. So _erbout << "string" is just concatenating the "string" onto the erb buffer at the current position in the erb template. So you can use this without worri=ying that it will break anytime soon but the other suggestions might be considered the ''rails'' way of doing things a bit more than the _erbout deal. But there are some situations that _erbout can be put to great use. HTH- -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Rich, On 9/28/05, Rich Duzenbury <rduz-6Hh2x1qHOZDQT0dZR+AlfA@public.gmane.org> wrote:> On Wed, 2005-09-28 at 19:41 -0700, Jeremy Kemper wrote: > > <ul> > > <% params.each do |key, value| -%> > > <li><%=h key %>: <%=h value %></li> > > <% end -%> > > </ul> > > > > Ahh, thanks for that. I suppose this can be achieved with the ''brace'' > block form of each on one line of code? I couldn''t make it work.Sure: <ul> <%= params.map {|key, value| content_tag :li, "#{h key}: #{h value}"} %> </ul> -- sam