Forum: Ruby on Rails How does this helper work? (agile book)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Al C. (Guest)
on 2007-03-24 01:38

Im trying to understand how this helper method works:

def hidden_div_if(condition, attributes = {})
    if condition
      attributes["style"] = "display: none"
    attrs = tag_options(attributes.stringify_keys)
    "<div #{attrs}>"

This is how they are calling it:

<%= hidden_div_if(@cart.items.empty?, :id => "cart") %>

Specifically, I need to know if the :id => "cart" is passed on to attrs
with the style attribute...

And also, what does the tag_options(attributes.stringify_keys) do?

Thanks in advance!
Stephen B. (Guest)
on 2007-03-24 02:15
(Received via mailing list)
Basically, tag_options() will create a set of html tag attributes from
a hash:

tag_options({"id" => "cart", "style" => "color: black"})

will give you:

id="cart" style="color: black"

which is convenient to use in a tag:

<div id="cart" style="color: black">

stringify_keys is a method for hashes that converts any symbol keys to
strings.  For example {:id => "cart"} becomes {"id" => "cart"}

Hope that helps,

This topic is locked and can not be replied to.