Forum: Ruby on Rails Menu Helper

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.
6727103d0ca3515130c97dd9e97900ab?d=identicon&s=25 Sam Schroeder (Guest)
on 2006-02-11 19:08
(Received via mailing list)
Hi all,

I'm trying to add a common menu to all pages in my application.  It is
just a set of links I want to display.  I'm having trouble deciding
where to put the code. Below is what I've tried to get out of the "Four
Days on Rails"  tutorial but no luck.  Any help would be appreciated. Am
I on the right track?

Thanks,

Sam

views/layout/application.rhtml (All I added was the method call to
"menu_display")
==============================
<html>
<head>
  <title>Test</title>
  <%= stylesheet_link_tag 'scaffold' %>
</head>
<body>
<%= menu_display %>
<p style="color: green"><%= flash[:notice] %></p>
<%= @content_for_layout %>
</body>
</html>

helpers/application_helper.rb (This is where the menu logic and display
would be)
=============================
module ApplicationHelper

  def menu_display
    # Security Logic to determine which menu option to display
  end

end

controllers/application.rb (linking the helper)
==========================
class ApplicationController < ActionController::Base

  helper :Application

end
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-02-11 19:32
(Received via mailing list)
I would say you are on the right track. Also, look into using partials
and such for this as well. I really hate putting html inside code
(even if it is just a helper), but that's just me.

Also, you dont need to add the helper to the application controller,
it will automatically pick it up.

-Nick
6727103d0ca3515130c97dd9e97900ab?d=identicon&s=25 Sam Schroeder (Guest)
on 2006-02-12 04:47
(Received via mailing list)
Nick,

Thanks for the help but I have another question.  I've filled out the
application_helper.rb file as follows.

module ApplicationHelper

  def menu_display
    # Security Logic to determine which menu option to dispay

    if session[:access_level] == "Admin"
      link_to '[Client Manager]', :action => 'list',   :controller =>
"Client"
      link_to '[User Manager]',   :action => 'list',   :controller =>
"User"
      link_to '[Logout]',         :action => 'logout', :controller =>
"User"
    elsif session[:access_level] != nil
      link_to '[Client Manager]', :action => 'list',   :controller =>
"Client"
      link_to '[Logout]',         :action => 'logout', :controller =>
"User"
    end
  end

end

The problem is that only the last link in ease case is displayed.  Any
ideas?

Thanks,

Sam
119af50160cabfe1fb6f2f05f5018c64?d=identicon&s=25 James Ludlow (Guest)
on 2006-02-12 07:58
(Received via mailing list)
On 2/11/06, Sam Schroeder <sschroeder@mn.rr.com> wrote:
>      if session[:access_level] == "Admin"
> "User"
>      end
>    end
>
>  end
>
>  The problem is that only the last link in ease case is displayed.  Any
> ideas?

I think you're confused about what a helper is.  It's a common
location for methods.  It's not a partial piece of the view.  The last
value in a method is returned automatically, which is why you're
getting that one back.  But for what you really want, a helper isn't
what you're looking for.

As was mentioned before, look into partials.

-- James
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-02-12 15:36
(Received via mailing list)
Yep, as James said. Helpers are so you can but some more complex logic
into your pages, but at the same time not "dirty" up the actual views.
You can do everything in the helper if you really want to, but the
link_to section would look something like:
  ...
  menu = ""
  menu << link_to '[Client Manager]', :action => 'list',   :controller
=> "Client"
  menu << link_to '[User Manager]',   :action => 'list',   :controller
=> "User"
  menu <<link_to '[Logout]',         :action => 'logout', :controller =>
"User"
  ...
  end

And again, as James said, the last thing to get executed is what ever
gets returned.

-Nick
6727103d0ca3515130c97dd9e97900ab?d=identicon&s=25 Sam Schroeder (Guest)
on 2006-02-12 22:00
(Received via mailing list)
So, to attempt to follows Rails best practices would it be better to do
which...

1. Make a method for each menu item in the the application_helper and
place a call to each in the application.rhtml?

or

2. Make an application level partial for the menus and put some logic in
that?  If so, where would I put this and reference it when in the
separate non-application level views?

or

3. Neither, because there is a better way.

Please pardon my ignorance.  I've only been getting into Rails for the
last week.  I really like it but the tutorials and examples I've found
are just not quite in-depth enough.  I mean they show off the
functionality great for a sample app but I'm needy and want more.

Again, thanks for your help.

Sam
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-02-12 22:22
(Received via mailing list)
On Sunday, February 12, 2006, at 2:59 PM, Sam Schroeder wrote:

>3. Neither, because there is a better way.

I have a quick menu setup on my app using a 'component'. p374-377 of
AWDR.
6727103d0ca3515130c97dd9e97900ab?d=identicon&s=25 Sam Schroeder (Guest)
on 2006-02-12 23:07
(Received via mailing list)
That is so creepy.  I was just reading those pages.  I'll give it a go.

Sam
This topic is locked and can not be replied to.