# Is there any better way to do this?

**URL:** <https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/156834>\
**Category:** Rails\
**Created:** [January 3, 2009, 9:04pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/156834 "2009-01-03T21:04:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 3, 2009, 9:04pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/156834/1 "2009-01-03T21:04:01Z")

</div>

Is there any better way to do this?

- \>

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 3, 2009, 9:37pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/156834/2 "2009-01-03T21:37:27Z")

</div>

Gi Ga wrote:

> Is there any better way to do this?
> 
> - \>

Here’s my take:

\<%= content\_tag :li, (:class =\> “current” unless current\_page?  
(:controller =\> ‘users’, :action =\> ‘login’)) %\>

I’ve not actually tried it though.

Bill

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 3, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/156834/3 "2009-01-03T21:40:55Z")

</div>

Bill D. wrote:

> Gi Ga wrote:
> 
> > Is there any better way to do this?
> > 
> > - \>
> 
> Here’s my take:
> 
> \<%= content\_tag :li, (:class =\> “current” unless current\_page?  
> (:controller =\> ‘users’, :action =\> ‘login’)) %\>
> 
> I’ve not actually tried it though.
> 
> Bill

Now that I look again, I think the “unless” needs to be “if”.

Bill

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 4, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/156834/4 "2009-01-04T15:14:43Z")

</div>

> - \>

First, move “if controller.controller\_name == ‘users’ &&  
controller.action\_name  
== ‘login’” into a helper and give it a name, such as login\_page\_class.  
It  
should return nil on the wrong page and ‘current’ on the right page.

Next, use content\_tag to build the entire li. Then if :class =\>  
login\_page\_class  
contains the nil, the class="" itself will go away in the rendered HTML.

(And put all this under test, because it’s logic that you want to keep  
alive as  
you upgrade this site!)

–  
Phlip

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 4, 2009, 8:57am UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/156834/5 "2009-01-04T08:57:14Z")

</div>

\<li\<% if controller.controller\_name == ‘users’ &&  
controller.action\_name == ‘login’ %\> class=“current”\<% end %\>\>

Probably safe syntax. I usually don’t like to print things inside of  
operations. Plus, with this, you won’t get any unnecessary whitespace.

But, there is no right way. As long as that Ruby interpreter does it’s  
job and doesn’t blow up in your face!
