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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5b20b09d5d24bd554f703ff0595696b?d=identicon&s=25 Al Cholic (desertfox)
on 2007-03-24 00:38
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!
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-03-24 01: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,

Steve
This topic is locked and can not be replied to.