# CSS tabs in RoR

**URL:** <https://www.ruby-forum.com/t/css-tabs-in-ror/48338>\
**Category:** Rails\
**Created:** [November 19, 2005, 1:56am UTC](https://www.ruby-forum.com/t/css-tabs-in-ror/48338 "2005-11-19T01:56:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![benedictsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@benedictsmith](https://www.ruby-forum.com/u/benedictsmith)\
**Post date:** [November 19, 2005, 1:56am UTC](https://www.ruby-forum.com/t/css-tabs-in-ror/48338/1 "2005-11-19T01:56:05Z")

</div>

I’ve just implemented some CSS tabs into my application (from here:  
[http://labs.silverorange.com/archives/2004/may/updatedsimple](http://labs.silverorange.com/archives/2004/may/updatedsimple)), but to  
display  
properly it requires that at least one item in the list is rather  
than a  
normal link, which I managed using this helper from [leapingfrogs.com](http://leapingfrogs.com):

def link\_to\_unless\_current\_with\_current\_class(\*args)  
link\_to\_unless\_current(\*args) {|name| “#{name}”}  
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.

---

<div class="post-metadata">

**Author:** ![benedictsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@benedictsmith](https://www.ruby-forum.com/u/benedictsmith)\
**Post date:** [November 19, 2005, 3:02am UTC](https://www.ruby-forum.com/t/css-tabs-in-ror/48338/2 "2005-11-19T03:02:22Z")

</div>

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|  
“#{name}”  
end  
end

end

In the view

\<%

# List of all the controllers here.

[ PostController,  
AuthorController,  
Admin::AuthorsController ].each do |c\_class|  
%\>

- \<% 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 }) %\> 
\<% end %\>

Regards,  
Blair

–  
Blair Z., Ph.D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subversion and Orca training and consulting  
[http://www.orcaware.com/svn/](http://www.orcaware.com/svn/)

---

<div class="post-metadata">

**Author:** ![benedictsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@benedictsmith](https://www.ruby-forum.com/u/benedictsmith)\
**Post date:** [November 19, 2005, 4:41am UTC](https://www.ruby-forum.com/t/css-tabs-in-ror/48338/3 "2005-11-19T04:41:49Z")

</div>

Blair Z. \<blair@…\> writes:

> ```
> { :controller => '/' +
> c_class.controller_path }) %>
> 
> ```
> 
> \<% 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.

---

<div class="post-metadata">

**Author:** ![benedictsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@benedictsmith](https://www.ruby-forum.com/u/benedictsmith)\
**Post date:** [November 19, 2005, 4:19pm UTC](https://www.ruby-forum.com/t/css-tabs-in-ror/48338/4 "2005-11-19T16:19:18Z")

</div>

> 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 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](http://img392.imageshack.us/img392/3634/tabs8mo.png)

Thanks again.

---

<div class="post-metadata">

**Author:** ![benedictsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@benedictsmith](https://www.ruby-forum.com/u/benedictsmith)\
**Post date:** [November 19, 2005, 6:09am UTC](https://www.ruby-forum.com/t/css-tabs-in-ror/48338/5 "2005-11-19T06:09:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![benedictsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@benedictsmith](https://www.ruby-forum.com/u/benedictsmith)\
**Post date:** [November 20, 2005, 9:23pm UTC](https://www.ruby-forum.com/t/css-tabs-in-ror/48338/6 "2005-11-20T21:23:26Z")

</div>

Ben wrote:

> > > of finding the current controller?
> 
> The code works without an error, producing the tabs nicely, but it simply  
> doesn’t produce the 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](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:

 \<% [PostController, Admin::AuthorsController].each do |c\_class| %\> - \<% c\_human\_name = c\_class.name.demodulize[0..-11].underscore.humanize %\> \<% if controller.kind\_of?(c\_class) %\> \<% action\_name = controller.action\_name.to\_sym %\> \<%= c\_human\_name %\> 
  - \<% if [:index, :list].include?(action\_name) %\> List \<% else %\> \<%= link\_to 'List', :action =\> :list %\> \<% end %\> 
  - \<% if :new == action\_name %\> New \<% else %\> \<%= link\_to 'New', :action =\> :new %\> \<% end %\> 

 \<% else %\> \<%= link\_to c\_human\_name, { :controller =\> '/' + c\_class.controller\_path } %\> \<% end %\> 
 \<% end %\> 

Regards,  
Blair
