Forum: Ruby on Rails Conditional statements in rhtml

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
93d2b6696403e856cdbb7266223126dd?d=identicon&s=25 Ro Vent (rvent)
on 2007-01-24 19:10
Hello i was wondering is there was any way of using an IF statement in a
.rhtml..?

So far i have this:
<div class='bar2' id='barpo6'><strong>State: </strong><%= if
@boards.running == "1" render :text => "Running" %></div>

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
8e73782b5a220c5d923a0195667e7406?d=identicon&s=25 Kim (Guest)
on 2007-01-24 20:29
(Received via mailing list)
You can embed any ruby code in RHTML - that is what the R stands for -
by putting the code in between <% %> or <%=%>

Try this:

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

you might be able to embed the whole code block in a <%=%> - give it a
try.
93d2b6696403e856cdbb7266223126dd?d=identicon&s=25 Ro Vent (rvent)
on 2007-01-24 22:40
Hummm.. That didnt work... It didnt give me any errors, but the test
wont show up...

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

I also tried:
    <div class='bar2' id='barpo6'><strong>State: </strong><%= 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
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2007-01-25 03:29
(Received via mailing list)
> Hello i was wondering is there was any way of using an IF statement in a
> .rhtml..?
>
> So far i have this:
> <div class='bar2' id='barpo6'><strong>State: </strong><%= if
> @boards.running == "1" render :text => "Running" %></div>
<...>


<%= @boards.running==1?'Running':'Waiting' %>

Regards,
Rimantas
--
http://rimantas.com/
93d2b6696403e856cdbb7266223126dd?d=identicon&s=25 Ro Vent (rvent)
on 2007-01-25 17:26
Rimantas Liubertas wrote:
>
>
> <%= @boards.running==1?'Running':'Waiting' %>
>
> Regards,
> Rimantas
> --
> 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...
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-01-25 17:36
(Received via mailing list)
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  <-- NOT a blog!!
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-01-25 17:47
(Received via mailing list)
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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2007-01-25 17:59
(Received via mailing list)
>  >> 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/
This topic is locked and can not be replied to.