I''ve just implemented some CSS tabs into my application (from here: http://labs.silverorange.com/archives/2004/may/updatedsimple), but to display properly it requires that at least one item in the list is <span> rather than a normal link, which I managed using this helper from leapingfrogs.com: def link_to_unless_current_with_current_class(*args) link_to_unless_current(*args) {|name| "<span>#{name}</span>"} end (which goes in module ApplicationHelper, if you fancy trying this yourself). It works perfectly but when you move onto a subitem the rails helper acts properly and makes your menu tab back into a link, spoiling the formatting. What I''m looking for is an easy means of keeping the link to the controller as text, regardless of which action is being accessed. I think this is possible using something like link_to_unless (:controller = current controller...) but I''m not sure of the formatting. Can anyone please help? Thanks in advance.
Ben wrote:> I''ve just implemented some CSS tabs into my application (from here: > http://labs.silverorange.com/archives/2004/may/updatedsimple), but to display > properly it requires that at least one item in the list is <span> rather than a > normal link, which I managed using this helper from leapingfrogs.com: > > def link_to_unless_current_with_current_class(*args) > link_to_unless_current(*args) {|name| "<span>#{name}</span>"} > end > > (which goes in module ApplicationHelper, if you fancy trying this yourself). It > works perfectly but when you move onto a subitem the rails helper acts properly > and makes your menu tab back into a link, spoiling the formatting. > > What I''m looking for is an easy means of keeping the link to the controller as > text, regardless of which action is being accessed. I think this is possible > using something like link_to_unless (:controller = current controller...) but > I''m not sure of the formatting. Can anyone please help?Wow, thanks for the link to the tabs. I had some poor tabs before I saw them. Here''s how you can do this: module ApplicationHelper def link_to_unless_current_controller(controller_class, *args) link_to_unless(controller.kind_of?(controller_class), *args) do |name| "<span>#{name}</span>" end end end In the view <% # List of all the controllers here. [ PostController, AuthorController, Admin::AuthorsController ].each do |c_class| %> <li> <% c_human_name = c_class.name.demodulize[0..-11].underscore.humanize %> <%= link_to_unless_current_controller(c_class, c_human_name, { :controller => ''/'' + c_class.controller_path }) %> </li> <% end %> Regards, Blair -- Blair Zajac, Ph.D. <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> Subversion and Orca training and consulting http://www.orcaware.com/svn/
Blair Zajac <blair@...> writes:> <% > # List of all the controllers here. > [ PostController, > AuthorController, > Admin::AuthorsController ].each do |c_class| > %> > <li> > <% c_human_name = c_class.name.demodulize[0..-11].underscore.humanize %> > <%= link_to_unless_current_controller(c_class, > c_human_name, > { :controller => ''/'' + > c_class.controller_path }) %> > </li> > <% end %>>I''m incredibly grateful for your help, but I''m afraid I just can''t get the code working properly. It run without an error, but simply never produces a span instead of a link, so I guess the condition isn''t firing. Also, is there a way to test if something is a controller without needing the full controller name? What''s the simplest method of finding the current controller? Thanks again.
Ben wrote:> Blair Zajac <blair@...> writes: > > >> <% >> # List of all the controllers here. >> [ PostController, >> AuthorController, >> Admin::AuthorsController ].each do |c_class| >> %> >> <li> >> <% c_human_name = c_class.name.demodulize[0..-11].underscore.humanize %> >> <%= link_to_unless_current_controller(c_class, >> c_human_name, >> { :controller => ''/'' + >> c_class.controller_path }) %> >> </li> >> <% end %> > > > I''m incredibly grateful for your help, but I''m afraid I just > can''t get the code working properly. It run without an error, > but simply never produces a span instead of a link, so I guess > the condition isn''t firing.What''s the error? You did change the list of controller classes, right?> Also, is there a way to test if something is a controller > without needing the full controller name? What''s the simplest method > of finding the current controller?You can check if something is a controller via object.instance_of?(ApplicationController). But you should not need this, as you should know the class of your variables. The current controller is always in ''controller'' in your views and actions. Regards, Blair
> What''s the error? You did change the list of controller classes, right?Yes, and there is no error!> > > Also, is there a way to test if something is a controller > > without needing the full controller name? What''s the simplest method > > of finding the current controller? > > You can check if something is a controller via > object.instance_of?(ApplicationController). But you should not need this, as > you should know the class of your variables. > > The current controller is always in ''controller'' in your views and actions. > > Regards, > Blair >The code works without an error, producing the tabs nicely, but it simply doesn''t produce the <span> when on current controller - here''s an image (with message as the current controller, it should be plain text rather then still a link) http://img392.imageshack.us/img392/3634/tabs8mo.png Thanks again.
Ben wrote:> > >>What''s the error? You did change the list of controller classes, right? > > > Yes, and there is no error! > > >>>Also, is there a way to test if something is a controller >>>without needing the full controller name? What''s the simplest method >>>of finding the current controller? >> >>You can check if something is a controller via >>object.instance_of?(ApplicationController). But you should not need this, as >>you should know the class of your variables. >> >>The current controller is always in ''controller'' in your views and actions. >> >>Regards, >>Blair >> > > > > The code works without an error, producing the tabs nicely, but it simply > doesn''t produce the <span> when on current controller - here''s an image (with > message as the current > controller, it should be plain text rather then still a link) > http://img392.imageshack.us/img392/3634/tabs8mo.pngDo you mean the current action or current controller? In any case, here''s the code I used in the end. I removed the method I added to the helper, as the code was not needed here: <div id="header"> <ul id="primary"> <% [ PostController, Admin::AuthorsController ].each do |c_class| %> <li> <% c_human_name = c_class.name.demodulize[0..-11].underscore.humanize %> <% if controller.kind_of?(c_class) %> <% action_name = controller.action_name.to_sym %> <span> <%= c_human_name %> </span> <ul id="secondary"> <li> <% if [ :index, :list ].include?(action_name) %> <span>List</span> <% else %> <%= link_to ''List'', :action => :list %> <% end %> </li> <li> <% if :new == action_name %> <span>New</span> <% else %> <%= link_to ''New'', :action => :new %> <% end %> </li> </ul> <% else %> <%= link_to c_human_name, { :controller => ''/'' + c_class.controller_path } %> <% end %> </li> <% end %> </ul> </div> Regards, Blair