How does this helper work? (agile book)


#1

Hello,

Im trying to understand how this helper method works:

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

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!


#2

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:

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

Hope that helps,

Steve