If-less if statements

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

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

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.

Hi Shandy,

On Thu, 20 Mar 2008 09:01:48 -0500
Shandy N. [email protected] 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,

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.

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

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

<%= 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

On Fri, Mar 21, 2008 at 3:41 PM, Shandy N. [email protected]
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.