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
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.
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.
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.
So, to attempt to follows Rails best practices would it be better to do
which…
Make a method for each menu item in the the application_helper and
place a call to each in the application.rhtml?
or
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.