Forum: Ruby on Rails What's a smart way....

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
JannaB (Guest)
on 2009-06-02 18:17
(Received via mailing list)
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
Mk 2. (Guest)
on 2009-06-02 19:13
Make "administrator" a link_to_remote and use :update:

link_to_remote "Administrator", :update => 'Body' :url => { :action =>
...
JannaB (Guest)
on 2009-06-02 19:18
(Received via mailing list)
But I want to only change the menu bar on that. This doesnt do that.
Mk 2. (Guest)
on 2009-06-02 19:51
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?
JannaB (Guest)
on 2009-06-02 20:16
(Received via mailing list)
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 = '<ul id="navlist">'
      tabs.each do |tab|
        html += '<li>'
        html += tab
        html += '</li>'
      end
    end

end
Mk 2. (Guest)
on 2009-06-02 20:45
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
This topic is locked and can not be replied to.