Menu Helper

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”)

Test <%= stylesheet_link_tag 'scaffold' %> <%= menu_display %>

<%= flash[:notice] %>

<%= @content_for_layout %>

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

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

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

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

On 2/11/06, Sam S. [email protected] 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

On Sunday, February 12, 2006, at 2:59 PM, Sam S. wrote:

  1. Neither, because there is a better way.

I have a quick menu setup on my app using a ‘component’. p374-377 of
AWDR.

That is so creepy. I was just reading those pages. I’ll give it a go.

Sam

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

  1. 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

  1. 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