Forum: Ruby on Rails CSS tabs in RoR

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
benedictsmith (Guest)
on 2005-11-19 02:56
(Received via mailing list)
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.
blair (Guest)
on 2005-11-19 04:02
(Received via mailing list)
Ben wrote:
> 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 Z., Ph.D.
<removed_email_address@domain.invalid>
Subversion and Orca training and consulting
http://www.orcaware.com/svn/
benedictsmith (Guest)
on 2005-11-19 05:41
(Received via mailing list)
Blair Z. <blair@...> writes:


>                                          { :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.
blair (Guest)
on 2005-11-19 07:09
(Received via mailing list)
Ben wrote:
>>    <% c_human_name = c_class.name.demodulize[0..-11].underscore.humanize %>
> 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
benedictsmith (Guest)
on 2005-11-19 17:19
(Received via mailing list)
> What's the error?  You did change the list of controller classes, right?

Yes, and there is no error!

>
> 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.
blair (Guest)
on 2005-11-20 22:23
(Received via mailing list)
Ben wrote:
>>>of finding the current controller?
>
>
>
> 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

Do 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
This topic is locked and can not be replied to.