if-less if statements

S

Shandy Nantz

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
 
D

David A. Black

Hi --

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,

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


David
 
S

Shandy Nantz

David said:
Hi --



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.
 
P

Paul Mckibbin

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.
 
P

Paul Mckibbin

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
 
D

David A. Black

Hi --

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).

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


David
 
S

Shandy Nantz

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

Jesús Gabriel y Galán

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,111
Latest member
PorterZ31

Latest Threads

Top