# Helper

**URL:** <https://www.ruby-forum.com/t/helper/160918>\
**Category:** Rails\
**Created:** [February 20, 2009, 10:55pm UTC](https://www.ruby-forum.com/t/helper/160918 "2009-02-20T22:55:21Z")\
**Posts on this page:** 7\
**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:** [February 20, 2009, 10:55pm UTC](https://www.ruby-forum.com/t/helper/160918/1 "2009-02-20T22:55:21Z")

</div>

How make a helper:

If value X is true: print

- else 
-

---

<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:** [February 20, 2009, 10:59pm UTC](https://www.ruby-forum.com/t/helper/160918/2 "2009-02-20T22:59:13Z")

</div>

Try something like this:

def current\_li(content, x)  
klass = x ? “current” : nil  
content\_tag(:li, content, :class =\> klass)  
end

On Feb 20, 3:55 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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:** [February 20, 2009, 11:20pm UTC](https://www.ruby-forum.com/t/helper/160918/3 "2009-02-20T23:20:05Z")

</div>

Andrew B. wrote:

> def current\_li(content, x)  
> klass = x ? “current” : nil  
> content\_tag(:li, content, :class =\> klass)  
> end

Thanks, but why this doesn’t work?

\<%= current\_li {link\_to “xxx”, :controller =\> “yyy”}, @x == 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:** [February 20, 2009, 11:33pm UTC](https://www.ruby-forum.com/t/helper/160918/4 "2009-02-20T23:33:22Z")

</div>

On Feb 20, 10:20 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Andrew B. wrote:
> 
> > def current\_li(content, x)  
> > klass = x ? “current” : nil  
> > content\_tag(:li, content, :class =\> klass)  
> > end
> 
> Thanks, but why this doesn’t work?
> 
> \<%= current\_li {link\_to “xxx”, :controller =\> “yyy”}, @x == 1 %\>

because you’re passing a block that generates a link to . something  
like  
current\_li link\_to(…), @x == 1

would probably work.

Fred

---

<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:** [February 24, 2009, 9:04am UTC](https://www.ruby-forum.com/t/helper/160918/5 "2009-02-24T09:04:28Z")

</div>

And same for ?

selected\_option (value, content, x)

if x = true then

content

else

content

---

<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:** [February 24, 2009, 9:44am UTC](https://www.ruby-forum.com/t/helper/160918/6 "2009-02-24T09:44:31Z")

</div>

Frederick C. wrote:

> Come on, should be easy enough if you got the previous one 🙂

Not for me 🙂  
I can’t make it work:

def current\_option(value, content, x)  
selec = x ? “selected” : nil  
content\_tag(:option, content, :value =\> value, :selected =\> selec)  
end

---

<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:** [February 24, 2009, 9:17am UTC](https://www.ruby-forum.com/t/helper/160918/7 "2009-02-24T09:17:16Z")

</div>

On Feb 24, 8:04 am, Jo Jo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And same for ?

Come on, should be easy enough if you got the previous one 🙂 (not to  
mention that options\_for\_select etc. do this for you).

Fred
