# Conditional statements in rhtml

**URL:** <https://www.ruby-forum.com/t/conditional-statements-in-rhtml/83260>\
**Category:** Rails\
**Created:** [January 24, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/conditional-statements-in-rhtml/83260 "2007-01-24T19:10:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 24, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/conditional-statements-in-rhtml/83260/1 "2007-01-24T19:10:36Z")

</div>

Hello i was wondering is there was any way of using an IF statement in a  
.rhtml…?

So far i have this:

**State:** \<%= if @boards.running == "1" render :text =\> "Running" %\>

What i want to do is to evaluate a column’s value and depending on its  
value render a text. On the line above i want to render Running if  
@boards.running is == 1 else render Waiting…?

Can i do that the way i looking at it…?

Any ideas…

Thanks

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 24, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/conditional-statements-in-rhtml/83260/2 "2007-01-24T20:29:26Z")

</div>

You can embed any ruby code in RHTML - that is what the R stands for -  
by putting the code in between \<% %\> or \<%=%\>

Try this:

**State:** \<%if @boards.running == "1" %\> \<%= render :text =\> "Running" %\> \<%else%\> \<%= render :text =\> "Waiting" %\> \<%end%\>

you might be able to embed the whole code block in a \<%=%\> - give it a  
try.

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 24, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/conditional-statements-in-rhtml/83260/3 "2007-01-24T22:40:02Z")

</div>

Hummm… That didnt work… It didnt give me any errors, but the test  
wont show up…

here is what i have:

**State:**   
\<% if @boards.running == “1” %\>  
\<%= render :text =\> “Running” %\>  
\<% else %\>  
\<%= render :text =\> “Waiting” %\>  
\<% end %\>  

I also tried:

**State:** \<%= render  
:action =\> ‘state’ %\>

and in the controller i have  
def state  
bstate = Boards.find(@params[“id”])

```
        if bstate.running == "1"
            render :text => "Running"
        else
            render :text => "Waiting"
        end
    end

```

None of them worked, yet it didnt give me any errors…

Any ideas…?

Thanks

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 25, 2007, 3:29am UTC](https://www.ruby-forum.com/t/conditional-statements-in-rhtml/83260/4 "2007-01-25T03:29:25Z")

</div>

> Hello i was wondering is there was any way of using an IF statement in a  
> .rhtml…?
> 
> So far i have this:
> 
> **State:** \<%= if @boards.running == "1" render :text =\> "Running" %\>

\<…\>

\<%= @boards.running==1?‘Running’:‘Waiting’ %\>

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 25, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/conditional-statements-in-rhtml/83260/5 "2007-01-25T17:36:57Z")

</div>

Ro Vent wrote:

> ```
> <div class='bar2' id='barpo6'><strong>State: </strong>
> <% if @boards.running == "1" %>
> <%= render :text => "Running" %>
> <% else %>
> <%= render :text => "Waiting" %>
> <% end %>
> </div>
> 
> ```

The excess %\>\<% freak me out.

```
     <%= render :text => if @boards.running == "1"
         "Running"
     else
         "Waiting"
     end %>

```

Idiomatic but legible!

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 25, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/conditional-statements-in-rhtml/83260/6 "2007-01-25T17:47:00Z")

</div>

On Jan 25, 2007, at 11:26 AM, Ro Vent wrote:

> Cool, thanks… But on my previous post i did the same thing, you
> 
> It is basically the same thing…

> > 1==“1”  
> > =\> false

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

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 25, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/conditional-statements-in-rhtml/83260/7 "2007-01-25T17:59:03Z")

</div>

> > > 1==“1”  
> > > =\> false

That’s one thing. Second: you shouldn’t use render :text in .rhtml  
templates. I does not work there (AFAIK).

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![rvent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rvent](https://www.ruby-forum.com/u/rvent)\
**Post date:** [January 25, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/conditional-statements-in-rhtml/83260/8 "2007-01-25T17:26:58Z")

</div>

Rimantas L. wrote:

> \<%= @boards.running==1?‘Running’:‘Waiting’ %\>
> 
> ## Regards, Rimantas
> 
> [http://rimantas.com/](http://rimantas.com/)

Cool, thanks… But on my previous post i did the same thing, you just  
made it more elegant why woudnt it work…?

```
<div class='bar2' id='barpo6'><strong>State: </strong>
    <% if @boards.running == "1" %>
        <%= render :text => "Running" %>
    <% else %>
        <%= render :text => "Waiting" %>
    <% end %>
</div>

```

It is basically the same thing…
