# HAML: %li{:class =\> 'current' if page == 2} does not work?

**URL:** <https://www.ruby-forum.com/t/haml-li-class-current-if-page-2-does-not-work/129369>\
**Category:** Rails\
**Created:** [February 17, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/haml-li-class-current-if-page-2-does-not-work/129369 "2008-02-17T20:05:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 17, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/haml-li-class-current-if-page-2-does-not-work/129369/1 "2008-02-17T20:05:56Z")

</div>

Hi all

I’m iterating over navigation page links using HAML. I want to assign  
the current page’s link the CSS class .current trying to use:

%li{:class =\> ‘current’ if params[:page] == i}

Sadly this doesn’t work. How can I achieve the wanted result?

Thanks for help,  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 17, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/haml-li-class-current-if-page-2-does-not-work/129369/2 "2008-02-17T21:37:56Z")

</div>

On Feb 17, 2008, at 11:05 AM, Joshua M. wrote:

> %li{:class =\> ‘current’ if params[:page] == i}

params are always returned as strings. Use params[:page].to\_i == i or  
params[:page] == i.to\_s. See if that works.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 18, 2008, 11:06am UTC](https://www.ruby-forum.com/t/haml-li-class-current-if-page-2-does-not-work/129369/3 "2008-02-18T11:06:25Z")

</div>

I use this line in HAML, and that works fine.

%li{:class =\> (“active” if item[:item] == active\_menu\_item)}

so maybe it’s the ( ) that do the trick?

On Feb 18, 10:56 am, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 18, 2008, 10:56am UTC](https://www.ruby-forum.com/t/haml-li-class-current-if-page-2-does-not-work/129369/4 "2008-02-18T10:56:50Z")

</div>

Steve R. wrote:

> On Feb 17, 2008, at 11:05 AM, Joshua M. wrote:
> 
> > %li{:class =\> ‘current’ if params[:page] == i}
> 
> params are always returned as strings. Use params[:page].to\_i == i or  
> params[:page] == i.to\_s. See if that works.

Thanks, but I guess I wasn’t specific enough. 😉

My problem is that HAML throws an error when having an “if” statement  
within %li{…}. Isn’t it allowed to use them?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [February 18, 2008, 11:26am UTC](https://www.ruby-forum.com/t/haml-li-class-current-if-page-2-does-not-work/129369/5 "2008-02-18T11:26:28Z")

</div>

Peter wrote:

> I use this line in HAML, and that works fine.
> 
> %li{:class =\> (“active” if item[:item] == active\_menu\_item)}
> 
> so maybe it’s the ( ) that do the trick?
> 
> On Feb 18, 10:56 am, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Yes, this seemed to be the problem. Thanks.
