# Conditionally assigning html id to link\_to (tabbed interface)

**URL:** <https://www.ruby-forum.com/t/conditionally-assigning-html-id-to-link-to-tabbed-interface/150673>\
**Category:** Rails\
**Created:** [October 11, 2008, 6:43am UTC](https://www.ruby-forum.com/t/conditionally-assigning-html-id-to-link-to-tabbed-interface/150673 "2008-10-11T06:43:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 11, 2008, 6:43am UTC](https://www.ruby-forum.com/t/conditionally-assigning-html-id-to-link-to-tabbed-interface/150673/1 "2008-10-11T06:43:39Z")

</div>

Hi all,  
I am looking to implement a tabbed interface. The tabs are mere  
link\_to links in the template. All i need to do is, check if a  
particular tab has been selected and if it has been selected, set  
the :id attribute to ‘current’. The css code will take care of the  
rest. I know the css works because when i explicitly set the :id =\>  
'current without any conditions, it shows in the browser.  
I have however tried many variations and all are throwing me syntax  
errors. Hope someone can give me the right syntax for this or at least  
show a better way of implementing this.

\<%= link\_to ‘Companies’, companies\_path(), :id=\>“current” unless  
controller.controller\_name != ‘Companies’ %\>  
\<%= link\_to ‘Companies’, companies\_path(), if  
controller.controller\_name == ‘Companies’ :id=\>“current” end%\>  
\<%= link\_to ‘Companies’, companies\_path(), if  
controller.controller\_name != ‘Companies’ :id=\>“current” %\>  
\<%= link\_to ‘Companies’, companies\_path(), if current\_page?  
(:controller=\> ‘Companies’) :id=\>“current” end%\>

I have seen the rails widget s plugin for tabbed interface and it just  
seems as too much code for something so simple.

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 11, 2008, 7:21am UTC](https://www.ruby-forum.com/t/conditionally-assigning-html-id-to-link-to-tabbed-interface/150673/2 "2008-10-11T07:21:40Z")

</div>

Vinay wrote:

> seems as too much code for something so simple.  
> Creating your own tab link helper ought to ease things up:

def tab\_link\_to(name, options = {}, html\_options = {})  
html\_options.merge!({ :id =\> ‘current’ }) if current\_page?(options)  
link\_to name, options, html\_options  
end

Which you could then use in your view:

\<% tab\_link\_to ‘Companies’, companies\_path %\>

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 11, 2008, 11:05am UTC](https://www.ruby-forum.com/t/conditionally-assigning-html-id-to-link-to-tabbed-interface/150673/3 "2008-10-11T11:05:39Z")

</div>

@Erol,  
Thank you so much! I have been letting this bug me for a couple of  
days now and to see your simple solution working is an amazing pick-me-  
up! 🙂  
I think its the simplest tabbed interface code one could hope for.

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 11, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/conditionally-assigning-html-id-to-link-to-tabbed-interface/150673/4 "2008-10-11T16:42:20Z")

</div>

You might also want to look at link\_to\_unless\_current (and perhaps its  
source code) to see how you might change your helper (or abandon it  
altogether in some cases).

-Rob

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 12, 2008, 12:44am UTC](https://www.ruby-forum.com/t/conditionally-assigning-html-id-to-link-to-tabbed-interface/150673/5 "2008-10-12T00:44:15Z")

</div>

check this out:  
[http://wiki.rubyonrails.org/rails/pages/Tabnav](http://wiki.rubyonrails.org/rails/pages/Tabnav)

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 11, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/conditionally-assigning-html-id-to-link-to-tabbed-interface/150673/6 "2008-10-11T18:49:24Z")

</div>

Thank you Rob.  
I actually DID come across these helpers when i was searching around  
for solutions. These though provide the link conditionally. If the  
condition is not satisfied, the link itself does not exist, it only  
renders text. But i need all the links to exist. And only the CSS id  
to change conditionally. For which the code above worked like a charm.  
Thanks again for your input!

On Oct 11, 7:41 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
