# Rails way to make a tag class active in view?

**URL:** <https://www.ruby-forum.com/t/rails-way-to-make-a-tag-class-active-in-view/105709>\
**Category:** Rails\
**Created:** [June 1, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/rails-way-to-make-a-tag-class-active-in-view/105709 "2007-06-01T16:55:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [June 1, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/rails-way-to-make-a-tag-class-active-in-view/105709/1 "2007-06-01T16:55:23Z")

</div>

for example 3 is in active state. right now all i can think of is in  
each action, just set a variable like @active = ‘active’ or ‘’, but not  
sure what to do with the span.

```
<ul>
  <!-- The link you call "active" will show up as a darker tab -->
  <li><a href="index.html">1</a></li>
  <li><a href="index.html">2</a></li>
  <li class="active"> <span>3</span> </li>
  <li><a href="index.html">4</a></li>
  <li><a href="index.html">5</a></li>
</ul>
```

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [June 1, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/rails-way-to-make-a-tag-class-active-in-view/105709/2 "2007-06-01T19:08:41Z")

</div>

On Jun 1, 2007, at 10:55 AM, mixplate wrote:

> ```
> <li><a href="index.html">4</a></li>
> <li><a href="index.html">5</a></li>
> </ul>
> 
> ```
> 
> –

Assuming that the “index.html” would actually be based on the 1, 2,  
etc.:

\<% for example in @examples %\>- \<%= link\_to\_unless\_current example, example\_path(example) do |text| content\_tag(:span, text, :class =\> 'active') end %\>
\<% end %\>

When @examples = [1,2,3,4,5]  
and example\_path(3) =\> “/example/3”

Gives:

- [1](https://www.ruby-forum.com/example/1)
[](https://www.ruby-forum.com/example/1)- [2](https://www.ruby-forum.com/example/2)
[ - 3
 ](https://www.ruby-forum.com/example/2)- [4](https://www.ruby-forum.com/example/4)
[](https://www.ruby-forum.com/example/4)- [5](https://www.ruby-forum.com/example/5)
[](https://www.ruby-forum.com/example/5)
[ 

Is that what you’re looking for? The link\_to\_if, link\_to\_unless,  
link\_to\_unless\_current helpers take a block that is passed the text  
of the link when the condition isn’t satisfied for making the link.

 

-Rob

 ](https://www.ruby-forum.com/example/5)

[Rob B.](https://www.ruby-forum.com/example/5)[http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [June 1, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/rails-way-to-make-a-tag-class-active-in-view/105709/3 "2007-06-01T19:08:41Z")

</div>

(resending without being signed, sorry if the original shows up)  
On Jun 1, 2007, at 10:55 AM, mixplate wrote:

> ```
> <li><a href="index.html">4</a></li>
> <li><a href="index.html">5</a></li>
> </ul>
> 
> ```
> 
> –

Assuming that the “index.html” would actually be based on the 1, 2,  
etc.:

\<% for example in @examples %\>- \<%= link\_to\_unless\_current example, example\_path(example) do |text| content\_tag(:span, text, :class =\> 'active') end %\>
\<% end %\>

When @examples = [1,2,3,4,5]  
and example\_path(3) =\> “/example/3”

Gives:

- [1](https://www.ruby-forum.com/example/1)
[](https://www.ruby-forum.com/example/1)- [2](https://www.ruby-forum.com/example/2)
[ - 3
 ](https://www.ruby-forum.com/example/2)- [4](https://www.ruby-forum.com/example/4)
[](https://www.ruby-forum.com/example/4)- [5](https://www.ruby-forum.com/example/5)
[](https://www.ruby-forum.com/example/5)
[ 

Is that what you’re looking for? The link\_to\_if, link\_to\_unless,  
link\_to\_unless\_current helpers take a block that is passed the text  
of the link when the condition isn’t satisfied for making the link.

 

-Rob

 ](https://www.ruby-forum.com/example/5)

[Rob B.](https://www.ruby-forum.com/example/5)[http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [June 1, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/rails-way-to-make-a-tag-class-active-in-view/105709/4 "2007-06-01T19:31:39Z")

</div>

I Rob,Thank you for your reply. Actually, the code i pasted would be  
more like this:

```
<ul>
  <!-- The link you call "active" will show up as a darker tab -->
  <li> <% link_to :action => get_category, :category => 'pork' %> 

```

- 3 
- \<% link\_to :action =\> get\_category, :category =\> 'beef' %\>

```
</ul>

```

so @examples would be an array of my category names?

Rob B. wrote:

> (resending without being signed, sorry if the original shows up)  
> On Jun 1, 2007, at 10:55 AM, mixplate wrote:
> 
> > ```
> > <li><a href="index.html">4</a></li>
> > <li><a href="index.html">5</a></li>
> > </ul>
> > 
> > ```
> > 
> > –
> 
> Assuming that the “index.html” would actually be based on the 1, 2,  
> etc.:
> 
> \<% for example in @examples %\>- \<%= link\_to\_unless\_current example, example\_path(example) do |text| content\_tag(:span, text, :class =\> 'active') end %\>
> \<% end %\>
> 
> When @examples = [1,2,3,4,5]  
> and example\_path(3) =\> “/example/3”
> 
> Gives:
> 
> - [1](https://www.ruby-forum.com/example/1)
> [](https://www.ruby-forum.com/example/1)- [2](https://www.ruby-forum.com/example/2)
> [ - 3
> ](https://www.ruby-forum.com/example/2)- [4](https://www.ruby-forum.com/example/4)
> [](https://www.ruby-forum.com/example/4)- [5](https://www.ruby-forum.com/example/5)
> [](https://www.ruby-forum.com/example/5)
> [ 
> 
> Is that what you’re looking for? The link\_to\_if, link\_to\_unless,  
> link\_to\_unless\_current helpers take a block that is passed the text  
> of the link when the condition isn’t satisfied for making the link.
> 
>  
> 
> -Rob
> 
> ](https://www.ruby-forum.com/example/5)
> 
> [Rob B.](https://www.ruby-forum.com/example/5)[http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [June 1, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/rails-way-to-make-a-tag-class-active-in-view/105709/5 "2007-06-01T19:46:46Z")

</div>

Well, if you need to have

- ... rather than
- 1
-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

> 

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
+1 513-295-4739  
Skype: rob.biedenharn
