Mountain Drummer
2006-Sep-11 16:46 UTC
Accessing name of controller and action from layout
Hi, I am generating a navigation bar on top of my page in the application layout. I want to set the class name for a navigation item to "current" if the current page displayed is associated with that navigation item so it can be made to look like it is selected by the stylesheet settings. What is the best way for me to access the name of the controller and the action so I could implement this logic in the layout? I tried the method controller_class_name, but it was an undefined variable. Is there a better way to structure this than what I am trying to do? Thanks, Ryan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jean-Etienne Durand
2006-Sep-11 16:53 UTC
Re: Accessing name of controller and action from layout
Mountain Drummer wrote:> Hi, > I am generating a navigation bar on top of my page in the application > layout. I want to set the class name for a navigation item to "current" > if the current page displayed is associated with that navigation item so > it can be made to look like it is selected by the stylesheet settings. > > What is the best way for me to access the name of the controller and the > action so I could implement this logic in the layout? > > I tried the method controller_class_name, but it was an undefined > variable. > > Is there a better way to structure this than what I am trying to do? > > Thanks, > > RyanHi Ryan, You can have some before_filter :set_names call in your application controller. Then those variables will be accessible from your views. def set_names @controller_name = controller_name() @action_name = action_name() end Just for info, there is a link_to_unless_current helper. Jean-Etienne -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
<%= self.controller.action_name.eql?(''my_page'') ? ''current'' : self.controller.action_name %> On 11/09/06, Jean-Etienne Durand <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Mountain Drummer wrote: > > Hi, > > I am generating a navigation bar on top of my page in the application > > layout. I want to set the class name for a navigation item to "current" > > if the current page displayed is associated with that navigation item so > > it can be made to look like it is selected by the stylesheet settings. > > > > What is the best way for me to access the name of the controller and the > > action so I could implement this logic in the layout? > > > > I tried the method controller_class_name, but it was an undefined > > variable. > > > > Is there a better way to structure this than what I am trying to do? > > > > Thanks, > > > > Ryan > > Hi Ryan, > > You can have some before_filter :set_names call in your application > controller. Then those variables will be accessible from your views. > > def set_names > @controller_name = controller_name() > @action_name = action_name() > end > > Just for info, there is a link_to_unless_current helper. > > Jean-Etienne > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Curtis Summers
2006-Sep-11 20:01 UTC
Re: Accessing name of controller and action from layout
You can also check the params like so: class="<%= (params[:controller] == ''my_page'' ? ''current'' : ''not-current'') %>" -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---