# Dynamic menu calling basic ajax area

**URL:** <https://www.ruby-forum.com/t/dynamic-menu-calling-basic-ajax-area/123732>\
**Category:** Rails\
**Created:** [December 20, 2007, 3:59am UTC](https://www.ruby-forum.com/t/dynamic-menu-calling-basic-ajax-area/123732 "2007-12-20T03:59:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 20, 2007, 3:59am UTC](https://www.ruby-forum.com/t/dynamic-menu-calling-basic-ajax-area/123732/1 "2007-12-20T03:59:29Z")

</div>

i was thinking, that for alot of my application, when i have a record, i  
want a menu with all the related ajax calls linked to a that record.  
there might me a gem to do this, or an easier way, but this is what i  
did. i am open to new ideas, as this was made to work easily from any  
view by calling a partial and setting up the ajax info in the helpers.  
what do you all think about this type of DRY? is there another better  
way? i got it working on link\_to but now working on link\_to\_remote.

views\>layout\>\_menu.rhtml

\<%= stylesheet\_link\_tag ‘ajaxmenu’ %\>

GET RELATED INFORMATION

```
<% makemenu %>

<% @menu.each do |menudetails| %>

    <li>
        <%= link_to_remote menudetails[:name],
        :update => 'ajaxarea',
        :url => { :controller => menudetails[:link_controller],

```

:action =\> menudetails[:link\_action] },  
:before =\> “$(‘waiting’).show()”,  
:success =\> “$(‘div\_part’).visualeffect(‘highlight’)”,  
:failure =\> “alert(‘There was a failure, GOOFY!.’)”,  
:complete =\> “$(‘waiting’).hide()”  
%\>

```
<% end %>

```

WORK IN PROGRESS…HOLD ON,  
SHUT UP, AND SIT DOWN!

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 20, 2007, 4:04am UTC](https://www.ruby-forum.com/t/dynamic-menu-calling-basic-ajax-area/123732/2 "2007-12-20T04:04:50Z")

</div>

So you want a different menu for different objects in your system?

May I recommend using different layouts for the different objects. Say  
you  
have a CandyBarsController, just define:

class CandyBarsController \< ApplicationController  
layout ‘candy\_bars’  
end

I think that you might not even have to define the layout if the  
controller  
name is the same as the layout name, but I’m not sure on that 🙂

On Dec 20, 2007 1:29 PM, gemblon (t.b.)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> \<% makemenu %\>  
> :failure =\> “alert(‘There was a failure, GOOFY!.’)”,
> 
> -- Posted via http://www.ruby-forum.com/.
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 20, 2007, 4:21am UTC](https://www.ruby-forum.com/t/dynamic-menu-calling-basic-ajax-area/123732/3 "2007-12-20T04:21:13Z")

</div>

Ryan B. wrote:

> So you want a different menu for different objects in your system?
> 
> May I recommend using different layouts for the different objects. Say  
> you  
> have a CandyBarsController, just define:
> 
> class CandyBarsController \< ApplicationController  
> layout ‘candy\_bars’  
> end
> 
> I think that you might not even have to define the layout if the  
> controller  
> name is the same as the layout name, but I’m not sure on that 🙂
> 
> –  
> Ryan B.  
> [http://www.frozenplague.net](http://www.frozenplague.net)

hi ryan,

well, you are a lot smarter then me, so i might not be doing this right.  
in each controller, i already have a layout template, that is pretty  
complex.  
i need to use that.

so, when i edit or show a record, i was gonna display the table data,  
and then, under that, show the menu for remote links.

is that not good?

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 20, 2007, 4:45am UTC](https://www.ruby-forum.com/t/dynamic-menu-calling-basic-ajax-area/123732/4 "2007-12-20T04:45:16Z")

</div>

Ryan B. wrote:

> Are you able to give me an example of what these remote links are?

oops, sorry ryan… the helper file has:

def makemenu  
@menu = []

```
    #an ajax menu
    item = {
      :name => "Notes",
      :link_controller => "casefilenote",
      :link_action => "list"
      }
    @menu << item

    #an ajax menu
    item = {
      :name => "Case Events",
      :link_controller => "event",
      :link_action => "list"
      }
    @menu << item

```

end

it all seems to work nice, and is really fast to add ajax menu items.  
well, for my application. you just have to watch with layout you use in  
a controller, depending on what method is going to be called.

is there a better way?

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 20, 2007, 4:23am UTC](https://www.ruby-forum.com/t/dynamic-menu-calling-basic-ajax-area/123732/5 "2007-12-20T04:23:32Z")

</div>

Are you able to give me an example of what these remote links are?

On Dec 20, 2007 1:51 PM, gemblon (t.b.)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > end
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 20, 2007, 4:54am UTC](https://www.ruby-forum.com/t/dynamic-menu-calling-basic-ajax-area/123732/6 "2007-12-20T04:54:24Z")

</div>

gemblon (t.b.) wrote:

> Ryan B. wrote:

and i need to track what menu was selected. so i can gray the menu  
background. and fix up a few more things.

i am installing in several places, and i will update it tomorrow if you  
want to see it in action. it is not perfectly clean yet, but that is  
just me doing some clean up.

🙂

if you have another idea, let me know. i will try it on a table.

🙂

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 20, 2007, 5:24am UTC](https://www.ruby-forum.com/t/dynamic-menu-calling-basic-ajax-area/123732/7 "2007-12-20T05:24:54Z")

</div>

We use something here to select which menu item is selected. I can’t  
show  
you the exact code, but basically you give the containing element of the  
link a class depending on an argument passed in.

def menu\_selected(menu\_item)  
if menu\_item == “edit” && params[:action] == “edit”  
“selected”  
end

Then on your menu:

- Edit
...

On Dec 20, 2007 2:24 PM, gemblon (t.b.)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> just me doing some clean up.
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![gemblon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ee59a6/32.png) [@gemblon](https://www.ruby-forum.com/u/gemblon)\
**Post date:** [December 20, 2007, 5:57am UTC](https://www.ruby-forum.com/t/dynamic-menu-calling-basic-ajax-area/123732/8 "2007-12-20T05:57:44Z")

</div>

Ryan B. wrote:

> We use something here to select which menu item is selected. I can’t  
> show  
> you the exact code, but basically you give the containing element of the  
> link a class depending on an argument passed in.
> 
> def menu\_selected(menu\_item)  
> if menu\_item == “edit” && params[:action] == “edit”  
> “selected”  
> end
> 
> Then on your menu:
> 
> - Edit
> ...
> 
> On Dec 20, 2007 2:24 PM, gemblon (t.b.)  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > just me doing some clean up.
> > 
> > >
> 
> –  
> Ryan B.  
> [http://www.frozenplague.net](http://www.frozenplague.net)

hey ryan, that seems like a cool idea.  
i am gonna give it a try tomorrow.

🙂
