Cannot call struct properties from HAML file
Posted
by
Lander
on Stack Overflow
See other posts from Stack Overflow
or by Lander
Published on 2012-10-24T04:47:32Z
Indexed on
2012/10/24
5:01 UTC
Read the original article
Hit count: 171
ruby-on-rails
|haml
I have the following code in my controller:
@nav_items = ActiveSupport::OrderedHash.new
@nav_items[:home] = Struct::NavItem.new("Home", nil, "/", "icon-home")
@nav_items[:about] = Struct::NavItem.new("About", nil, "/about", "icon-heart")
@nav_items[:contact] = Struct::NavItem.new("Contact", nil, "/contact", "icon-envelope")
if (current_user != nil && current_user.admin?)
@nav_items[:admin_divider] = Struct::NavItem.new(nil, "divider-vertical", nil, nil)
@nav_items[:admin] = Struct::NavItem.new("Admin", nil, "/admin", "")
end
And the following in my view:
- @nav_items.each do |nav_item|
%li{ :class => nav_item[:class] }
%a{ :href => nav_item[:link] }= nav_item[:text]
And my struct definition: Struct.new("NavItem", :text, :class, :link, :icon_class)
I'm relatively new to Ruby, Rails, and HAML, but in another project using ERB rendering, code like that worked fine. I've tried referencing properties by doing something like nav_item.link
as well, but that still does not work.
The error I get with my current code is:
Symbol as array index
By using code like nav_item.link
:
undefined method `link' for #< Array:0x126970ff0 >
As this is my first time using HAML, I'm not too sure what I'm doing wrong.
© Stack Overflow or respective owner