# Create a menu out from database entries

**URL:** <https://www.ruby-forum.com/t/create-a-menu-out-from-database-entries/149707>\
**Category:** Rails\
**Created:** [September 28, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/create-a-menu-out-from-database-entries/149707 "2008-09-28T19:41:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@dlc](https://www.ruby-forum.com/u/dlc)\
**Post date:** [September 28, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/create-a-menu-out-from-database-entries/149707/1 "2008-09-28T19:41:22Z")

</div>

Hi

I am starting to work with rails and like to build a very basic cms.

I was using scaffolding.  
Page title:string permalink:string body:text.

What I like to do is create a menu out of the database entries  
permalink.  
Those should link to the fitting pages.

So i modified my index.html.erb from the pages view like this:

\<% @pages.each do |page| %\>  
\<%= link\_to\_unless\_current page.permalink, page\_path(page) %\>  
\<% end %\>

I like to use something like the code above in a div layer as a menu  
and link the result to the div layer were the \<% yield %\> is placed.

I was trying to do this with helper module and also with partials, but  
I couldnt solve this.  
Do I need ajax? or is there a way to do it with rails only?

Maybe someone has a good tip?

Best regards

dlc

---

<div class="post-metadata">

**Author:** ![dlc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@dlc](https://www.ruby-forum.com/u/dlc)\
**Post date:** [September 28, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/create-a-menu-out-from-database-entries/149707/2 "2008-09-28T21:27:04Z")

</div>

On Sep 28, 10:31 am, dlc [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I couldnt solve this.  
> Do I need ajax? or is there a way to do it with rails only?
> 
> Maybe someone has a good tip?
> 
> Best regards
> 
> dlc

In ./app/helpers/application\_helper.rb:

def tab\_for(name, href, html\_options = {} )  
#puts ‘tab’, @current\_tab.to\_s, ‘href’, href.to\_s  
if @current\_tab.to\_s.starts\_with?(href.to\_s.downcase)  
#content\_tag :span, name, html\_options  
html\_options.merge!(:class =\> ‘active’)  
link\_to name, href, html\_options  
else  
link\_to name, href, html\_options  
end  
end

In the view layout:

- \<%= tab\_for 'Directories', directories\_path %\>
- \<%= tab\_for 'Software', softwares\_path %\>

The tab\_for function does a match on the request path to determine if  
its active or not…works well for single level menus, but would need  
modification to support sub menus.

---

<div class="post-metadata">

**Author:** ![dlc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@dlc](https://www.ruby-forum.com/u/dlc)\
**Post date:** [October 2, 2008, 12:58pm UTC](https://www.ruby-forum.com/t/create-a-menu-out-from-database-entries/149707/3 "2008-10-02T12:58:50Z")

</div>

Hi,

thanks a lot.  
I try to work that out.

Best

Daniel

---

<div class="post-metadata">

**Author:** ![dlc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@dlc](https://www.ruby-forum.com/u/dlc)\
**Post date:** [October 3, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/create-a-menu-out-from-database-entries/149707/4 "2008-10-03T22:22:27Z")

</div>

Hi,

glennswest wrote\>

> Actually got a good bit of code for this done.
> 
> 1. Scan a available controllers, and add table entries for any “new”  
> ones automatically
> 2. If updates, generate new tabnav menus automatically at startup.

Sounds like a good candidate for a plugin! Let us know if you go there.

Best regards,  
Bill
