# What's a smart way

**URL:** <https://www.ruby-forum.com/t/whats-a-smart-way/169341>\
**Category:** Rails\
**Created:** [June 2, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/whats-a-smart-way/169341 "2009-06-02T16:17:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [June 2, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/whats-a-smart-way/169341/1 "2009-06-02T16:17:43Z")

</div>

To do the following…

Suppose I have a list of links in a div of my main template,  
application.html.erb.

One of those links, the “administrator” link, needs to be able to  
change all the other links, so that a new set of links displays (with  
a “Back to application” link therein, to set the links back to the  
original way)

I’m trying to figure out a sane and simple way to do this (I am  
concocting lots of bad ways to do it!) Please help.

My application.html.erb appears here as:

```
   <div id="navcontainer">
     <%= links_for_navigation %>
      <div id="Body" >
        <%= yield %>
      </div>
    </div>

```

and then, in my applicationhelper file, I create the links themselves.  
How would you tackle this in an intelligent manner? I am making a mess  
of my code trying to do this. -Janna

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [June 2, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/whats-a-smart-way/169341/2 "2009-06-02T17:13:03Z")

</div>

Make “administrator” a link\_to\_remote and use :update:

link\_to\_remote “Administrator”, :update =\> ‘Body’ :url =\> { :action =\>  
…

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [June 2, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/whats-a-smart-way/169341/3 "2009-06-02T17:18:10Z")

</div>

But I want to only change the menu bar on that. This doesnt do that.

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [June 2, 2009, 5:51pm UTC](https://www.ruby-forum.com/t/whats-a-smart-way/169341/4 "2009-06-02T17:51:35Z")

</div>

JannaB wrote:

> But I want to only change the menu bar on that. This doesnt do that.

Okay, the specifics of your app are not clear to me. If you place your  
menu bar in a div (sorry, I guess that div was not “Body”), then you can  
swap the contents of the menu bar the same way.

For example, if you have one horizontal menu bar with three items that  
all have a drop down or sub-menu attached, you can actually use just  
_one_ drop down (or a box containing the active sub-menu). To change  
the contents of the active sub-menu (or drop-down), you use  
link\_to\_remote with all the menu bar options, :update’ing the div  
containing the sub-menu.

Those links don’t have to be in a separate menu (this I think is closer  
to what you are doing); they could be in the same menu. So when you  
click on the “administrator” option, the :update refers to the _same_  
div that contains the menu with the administrator option. Then the  
entire menu is replaced, and you have a “back to main menu” option which  
works the same way. Get it?

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [June 2, 2009, 6:16pm UTC](https://www.ruby-forum.com/t/whats-a-smart-way/169341/5 "2009-06-02T18:16:55Z")

</div>

Thank you! Yes, it is the same menu, I am simply trying to replace it.  
I dont care what else is on the screen, I just want to flip the menu.  
I am getting hung up on the syntax here though, I have:

link\_to\_remote( ‘Admin’, :update =\> ‘links\_for\_navigation’ ,nil )

the specific div where the menu(s) occur are in a div called  
‘links\_for\_navigation’ - but the syntax here of link\_to\_remote, when  
all I want to do is change what is in an individual dic in the  
application template, I am unable to discern – the rest of the page  
should merely remain the same.

Also, even if I can get the syntax right on this here, how do you set  
some kind of variable (session fariable perhaps?) to know within the  
ApplicationHelper which of the two menus to display? -Janna, my helper  
follows…

module ApplicationHelper

def nav\_link\_to(name, options, html\_options)

```
  link_to name, options, html_options
end

def links_for_navigation
  tabs = []
  if #I need some value here to determine which menu to

```

choose!

```
    tabs << link_to_remote( 'Admin', :update =>

```

‘links\_for\_navigation’ ,nil )

```
    tabs << nav_link_to('Reports', {:controller =>

```

‘customers’, :action =\> ‘list’}, {:title =\> ‘Go to Reports’, :style =\>  
‘float: right;’})  
tabs \<\< nav\_link\_to(‘Customers’, {:controller =\>  
‘customers’, :action =\> ‘list’}, {:title =\> ‘Manage Customer’, :style  
=\> ‘float: right;’})  
tabs \<\< nav\_link\_to(‘Scheduler’, {:controller =\>  
‘projects’, :action =\> ‘list’}, {:title =\> ‘Go to Scheduler’, :style  
=\> ‘float: right;’})  
tabs \<\< nav\_link\_to(‘InOut’, {:controller =\>  
‘associates’, :action =\> ‘list’}, {:title =\> ‘In Out Board’, :style =\>  
‘float: right;’)  
tabs \<\< nav\_link\_to(‘Uplog’, {:controller =\>  
‘customers’, :action =\> ‘list’}, {:title =\> ‘Access Uplog’, :style =\>  
‘float: right;’})  
else

tabs \<\< link\_to\_remote( ‘Back to Application’, :update =\>  
‘links\_for\_navigation’ ,nil )

```
    tabs << nav_link_to('Reasons', {:controller =>

```

‘reasons’, :action =\> ‘list’}, {:title =\> ‘Reasons’, :style =\> ‘float:  
right;’})  
tabs \<\< nav\_link\_to(‘Selections’, {:controller =\>  
‘selections’, :action =\> ‘list’}, {:title =\> ‘Selections’, :style =\>  
‘float: right;’})  
tabs \<\< nav\_link\_to(‘Activities’, {:controller =\>  
‘activities’, :action =\> ‘list’}, {:title =\> ‘Activities’, :style =\>  
‘float: right;’})  
tabs \<\< nav\_link\_to(‘ActivityTypes’, {:controller =\>  
‘activitytypes’, :action =\> ‘list’}, {:title =\> ‘Activity  
Types’, :style =\> ‘float: right;’})  
tabs \<\< nav\_link\_to(‘Status’, {:controller =\>  
‘status’, :action =\> ‘list’}, {:title =\> ‘Status’, :style =\> ‘float:  
right;’})  
tabs \<\< nav\_link\_to(‘Origins’, {:controller =\>  
‘origins’, :action =\> ‘list’}, {:title =\> ‘Origins’, :style =\> ‘float:  
right;’})  
tabs \<\< nav\_link\_to(‘Associates’, {:controller =\>  
‘associates’, :action =\> ‘list’}, {:title =\> ‘Associates’, :style =\>  
‘float: right;’})  
end  
html = ‘

’  
tabs.each do |tab|  
html += ‘- ’  
html += tab  
html += ‘
’  
end  
end

end

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [June 2, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/whats-a-smart-way/169341/6 "2009-06-02T18:45:25Z")

</div>

JannaB wrote:

> Thank you! Yes, it is the same menu, I am simply trying to replace it.  
> I dont care what else is on the screen, I just want to flip the menu.  
> I am getting hung up on the syntax here though, I have:
> 
> link\_to\_remote( ‘Admin’, :update =\> ‘links\_for\_navigation’ ,nil )

I can’t help you with the last part as I am quite new to RonR myself,  
altho I do have a programming background and can promise that I have  
already used link\_to\_remote to do this kind of menu substitution.

Here’s an example.

The syntax of link\_to\_remote would be:

link\_to\_remote “Admin”, :update =\> 'links\_for\_navigation,  
:url =\> { :action =\> :adminmenu }

Then, in your contoller, you have a method “adminmenu”

def adminmenu  
# provide variables needed for the menu  
@variables  
end

and of course you need a matching view which actually constructs the  
menu, ie. the “innerHTML” of the menu div:

## adminmenu.html.erb

\<% @variables.each do |option| %\>  
\<%=link\_to\_remote option.name, :update =\> option.target,  
:url =\> option.url%\>  
\<%end%\>

That is just a sketch; you can simplify using partials this way:

in the page source:  
link\_to\_remote “Admin”, :update =\> 'links\_for\_navigation,  
:url =\> { :action =\> :render\_partial, :partial = “adminmenu” }  
a generic method in the controller:  
def render\_partial  
render :partial =\> params[:partial]  
end

Then you just have a bunch of \_partials.html.erb for the different  
menus. But this last technique, AFAIK, is not standard and someone has  
implied there may be a security risk in it, so beware, I’m a ruby newbie  
too!

Good luck – MK
