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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060211/6a5b6c29/attachment.html
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 On 2/11/06, Sam Schroeder <sschroeder@mn.rr.com> wrote:> 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 > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
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 Nick Stuart wrote:>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 > >On 2/11/06, Sam Schroeder <sschroeder@mn.rr.com> wrote: > > >> 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 >> >> >> >> >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> >> > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060212/91fc77a6/attachment.html
On 2/11/06, Sam Schroeder <sschroeder@mn.rr.com> wrote:> 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?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
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/12/06, James Ludlow <jamesludlow@gmail.com> wrote:> On 2/11/06, Sam Schroeder <sschroeder@mn.rr.com> wrote: > > 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? > > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 Nick Stuart wrote:>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/12/06, James Ludlow <jamesludlow@gmail.com> wrote: > > >>On 2/11/06, Sam Schroeder <sschroeder@mn.rr.com> wrote: >> >> >>> 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? >>> >>> >>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 >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060212/b95005ff/attachment.html
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. -- Posted with http://DevLists.com. Sign up and save your time!
That is so creepy. I was just reading those pages. I''ll give it a go. Sam Kevin Olbrich wrote:>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. > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060212/a3bed53f/attachment.html