Items are being duplicated on render
Tricnic opened this issue · comments
I am trying to set up a managed navigation menu. I have Menu and Page models that determine what links should be shown in the menu.
class Menu < ActiveRecord::Base
attr_accessible :name, :slug
has_many :pages
end
class Page < ActiveRecord::Base
belongs_to :menu
attr_accessible :link, :order, :title, :menu_id
end
I am looping through the pages and having navigasmic render them.
<div id="navigation">
<%= semantic_navigation :primary do |n| %>
<% Menu.find_by_slug(:primary).pages.each do |page| %>
<%= n.item page.title, page.link %>
<% end %>
<% end %>
</div>
I only have 2 pages in the DB at the moment.
irb(main):002:0> Menu.find_by_slug(:primary).pages
Menu Load (1.0ms) SELECT "menus".* FROM "menus" WHERE "menus"."slug" = 'primary' LIMIT 1
Page Load (0.0ms) SELECT "pages".* FROM "pages" WHERE "pages"."menu_id" = 1
=> [#<Page id: 2, title: "Blog", link: "/posts", order: 1, menu_id: 1, created_at: "2013-01-26 16:10:51", updated_at: "2013-01-26 16:23:56">, #<Page id: 1, title: "New Book", link: "/books/new", order:0, menu_id: 1, created_at: "2013-01-26 16:06:43", updated_at: "2013-01-26 16:24:07">]
But, when I view the navigation container, it renders the first page 4 times and the second one twice.
<ul class="semantic-navigation" id="primary">
<li><a href="/posts"><span>Blog</span></a></li>
<li><a href="/posts"><span>Blog</span></a></li>
<li><a href="/books/new"><span>New Book</span></a></li>
<li><a href="/posts"><span>Blog</span></a></li>
<li><a href="/posts"><span>Blog</span></a></li>
<li><a href="/books/new"><span>New Book</span></a></li>
</ul>
I have no idea how it is behaving so oddly. Am I doing something wrong?
Check the readme for an example. It's because you're using the erb = where you shouldn't be.
Jeremy Jackson
On Jan 26, 2013, at 9:39 AM, Tricnic notifications@github.com wrote:
I am trying to set up a managed navigation menu. I have Menu and Page models that determine what links should be shown in the menu.
class Menu < ActiveRecord::Base attr_accessible :name, :slug has_many :pages end class Page < ActiveRecord::Base belongs_to :menu attr_accessible :link, :order, :title, :menu_id end
I am looping through the pages and having navigasmic render them.
<div id="navigation"> <%= semantic_navigation :primary do |n| %> <% Menu.find_by_slug(:primary).pages.each do |page| %> <%= n.item page.title, page.link %> <% end %> <% end %> </div>
I only have 2 pages in the DB at the moment.
irb(main):002:0> Menu.find_by_slug(:primary).pages Menu Load (1.0ms) SELECT "menus".* FROM "menus" WHERE "menus"."slug" = 'primary' LIMIT 1 Page Load (0.0ms) SELECT "pages".* FROM "pages" WHERE "pages"."menu_id" = 1 => [#<Page id: 2, title: "Blog", link: "/posts", order: 1, menu_id: 1, created_at: "2013-01-26 16:10:51", updated_at: "2013-01-26 16:23:56">, #<Page id: 1, title: "New Book", link: "/books/new", order:0, menu_id: 1, created_at: "2013-01-26 16:06:43", updated_at: "2013-01-26 16:24:07">]
But, when I view the navigation container, it renders the first page 4 times and the second one twice.
<ul class="semantic-navigation" id="primary"> <li><a href="/posts"><span>Blog</span></a></li> <li><a href="/posts"><span>Blog</span></a></li> <li><a href="/books/new"><span>New Book</span></a></li> <li><a href="/posts"><span>Blog</span></a></li> <li><a href="/posts"><span>Blog</span></a></li> <li><a href="/books/new"><span>New Book</span></a></li> </ul>
I have no idea how it is behaving so oddly. Am I doing something wrong?
—
Reply to this email directly or view it on GitHub.
Thank you. That fixed it right up. I guess I need to study the use of erb's = more.