# If-less if statements

**URL:** <https://www.ruby-forum.com/t/if-less-if-statements/132474>\
**Category:** Ruby\
**Created:** [March 20, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/if-less-if-statements/132474 "2008-03-20T15:01:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 20, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/if-less-if-statements/132474/1 "2008-03-20T15:01:55Z")

</div>

I asked this on the rails side of this forum but didn’t get a response  
and while I am using the code in a rails app I think that this is a ruby  
question.

In a helper that I am using, I am seeing if a phone # gets entered  
correctly, if not default text is entered in it’s place and displayed in  
red.

\<%= in\_place\_editor\_field ‘user’,‘w\_phone’,{ :style =\> @user.w\_phone ==  
‘Click  
to Edit’ ? ‘color:red;’ : ‘’}, :cols=\>20%\>

This highlets everything in that comes through with ‘Click to Edit’ in  
red. But what I would like to do is to hightlight everything in red if  
the string says either ‘Click to Edit’ of ‘Check Format’. So I tried  
this:

\<%= in\_place\_editor\_field ‘user’,‘w\_phone’,{ :style =\> @user.w\_phone ==  
‘Click  
to Edit’ ? ‘color:red;’ : @user.w\_phone == "Check Format " ?  
‘color:red;’  
: ‘’}, :cols=\>20%\>

This codes executes fine but it doesn’t show the ‘Check Format’ part as  
red, so I’m thinking that I don’t have the conditions set up wrong.  
Anyone have any suggestions? Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 20, 2008, 3:25pm UTC](https://www.ruby-forum.com/t/if-less-if-statements/132474/2 "2008-03-20T15:25:30Z")

</div>

Hi –

On Thu, 20 Mar 2008, Shandy N. wrote:

> to Edit’ ? ‘color:red;’ : ‘’}, :cols=\>20%\>  
> : ‘’}, :cols=\>20%\>
> 
> This codes executes fine but it doesn’t show the ‘Check Format’ part as  
> red, so I’m thinking that I don’t have the conditions set up wrong.  
> Anyone have any suggestions? Thanks,

It looks like you’ve got extra spaces: "Check Format ". Is that  
right?

David

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 20, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/if-less-if-statements/132474/3 "2008-03-20T15:54:25Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Thu, 20 Mar 2008, Shandy N. wrote:
> 
> > to Edit’ ? ‘color:red;’ : ‘’}, :cols=\>20%\>  
> > : ‘’}, :cols=\>20%\>
> > 
> > This codes executes fine but it doesn’t show the ‘Check Format’ part as  
> > red, so I’m thinking that I don’t have the conditions set up wrong.  
> > Anyone have any suggestions? Thanks,
> 
> It looks like you’ve got extra spaces: "Check Format ". Is that  
> right?
> 
> David

It’s for formatting, but I can get rid of it if that is causing a  
problem. Ruby on Rails was the book that I read when I was first  
learning rails - great book.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 21, 2008, 4:00am UTC](https://www.ruby-forum.com/t/if-less-if-statements/132474/4 "2008-03-21T04:00:45Z")

</div>

Hi Shandy,

On Thu, 20 Mar 2008 09:01:48 -0500  
Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> to Edit' ? 'color:red;' : ''}, :cols=>20%>
> 
> ```
> 
> This highlets everything in that comes through with ‘Click to Edit’ in  
> red. But what I would like to do is to hightlight everything in red if  
> the string says either ‘Click to Edit’ of ‘Check Format’. So I tried  
> this:
> 
> \<%= in\_place\_editor\_field ‘user’,‘w\_phone’,{ :style =\> @user.w\_phone  
> == ‘Click  
> to Edit’ ? ‘color:red;’ : @user.w\_phone == "Check Format " ?  
> ^^  
> I think what David’s asking is do you really want to be include those  
> 2 spaces in any comparison?

"Check Format ", and “Check Format” are different.

> ‘color:red;’  
> : ‘’}, :cols=\>20%\>
> 
> This codes executes fine but it doesn’t show the ‘Check Format’ part  
> as red, so I’m thinking that I don’t have the conditions set up wrong.  
> Anyone have any suggestions? Thanks,
> 
> -S

cheers,

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 21, 2008, 5:07am UTC](https://www.ruby-forum.com/t/if-less-if-statements/132474/5 "2008-03-21T05:07:43Z")

</div>

> On Thu, 20 Mar 2008 09:01:48 -0500
> 
> > This codes executes fine but it doesn’t show the ‘Check Format’ part  
> > as red, so I’m thinking that I don’t have the conditions set up wrong.  
> > Anyone have any suggestions? Thanks,

Why not use a regular expression? They’re fairly straightforward when  
you control the strings to this extent.

\<%= in\_place\_editor\_field ‘user’,‘w\_phone’,{ :style =\>  
(@user.w\_phone=~/Click to edit|Check format/) ? ‘color:red;’ : ‘’},  
:cols=\>20%\>

should do what you want regardless if there are extra spaces or not.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 21, 2008, 5:30am UTC](https://www.ruby-forum.com/t/if-less-if-statements/132474/6 "2008-03-21T05:30:32Z")

</div>

Paul M. wrote:

> > On Thu, 20 Mar 2008 09:01:48 -0500
> > 
> > > Anyone have any suggestions? Thanks,

Actually, and this is definitely more appropriate to the rails forum,  
you should really have this in you user\_helper.rb or  
application\_helper.rb file and called it from the view.

e.g.

In the view:

\<%= in\_place\_editor\_field ‘user’,‘w\_phone’,  
check\_red\_style(@user.w\_phone), :cols=\>20%\>

In the application\_helper.rb file:

module ApplicationHelper  
def check\_red\_style(input,test=/Click to edit|Check format/)  
(input=~test) ? {:style=\>‘color:red’} : {}  
end  
end

and then you can use whatever regular expression you want from the view  
(although check that you can have nested helpers, I don’t think it’s a  
problem, but I don’t have a scratch rails area to test it on).

Mac

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 21, 2008, 1:19pm UTC](https://www.ruby-forum.com/t/if-less-if-statements/132474/7 "2008-03-21T13:19:06Z")

</div>

Hi –

On Fri, 21 Mar 2008, Paul M. wrote:

> (input=~test) ? {:style=\>‘color:red’} : {}  
> end  
> end
> 
> and then you can use whatever regular expression you want from the view  
> (although check that you can have nested helpers, I don’t think it’s a  
> problem, but I don’t have a scratch rails area to test it on).

They’re just instance methods available to the current object, so you  
can have as any as you like.

David

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 21, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/if-less-if-statements/132474/8 "2008-03-21T15:41:26Z")

</div>

> \<%= in\_place\_editor\_field ‘user’,‘w\_phone’,  
> check\_red\_style(@user.w\_phone), :cols=\>20%\>
> 
> In the application\_helper.rb file:
> 
> module ApplicationHelper  
> def check\_red\_style(input,test=/Click to edit|Check format/)  
> (input=~test) ? {:style=\>‘color:red’} : {}  
> end  
> end

That worked beautifully, thank you. Just out of curiosity, can I do an  
if-elsif statement the way that I was trying? Can I have one conditional  
if statement within another to form a conditional if-elsif statement?  
Thanks,

-Shandy

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 22, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/if-less-if-statements/132474/9 "2008-03-22T12:15:18Z")

</div>

On Fri, Mar 21, 2008 at 3:41 PM, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Just out of curiosity, can I do an  
> if-elsif statement the way that I was trying? Can I have one conditional  
> if statement within another to form a conditional if-elsif statement?  
> Thanks,

Something like this?

irb(main):001:0\> a = false  
=\> false  
irb(main):002:0\> b = true  
=\> true  
irb(main):003:0\> a ? “a is true” : b ? “b is true” : “b is false”  
=\> “b is true”

Jesus.
