[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

if-less if statements

Shandy Nantz

3/20/2008 2:02:00 PM

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
--
Posted via http://www.ruby-....

8 Answers

David A. Black

3/20/2008 2:24:00 PM

0

Hi --

On Thu, 20 Mar 2008, Shandy Nantz wrote:

> 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

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.r... for details. Berlin dates coming soon!

Shandy Nantz

3/20/2008 2:54:00 PM

0

David A. Black wrote:
> Hi --
>
> On Thu, 20 Mar 2008, Shandy Nantz 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.

--
Posted via http://www.ruby-....

Mark Woodward

3/21/2008 2:57:00 AM

0

Hi Shandy,

On Thu, 20 Mar 2008 09:01:48 -0500
Shandy Nantz <shandybleu@yahoo.com> wrote:

> 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 " ?
^^
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,

--
Mark

Paul Mckibbin

3/21/2008 4:08:00 AM

0

> 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.
--
Posted via http://www.ruby-....

Paul Mckibbin

3/21/2008 4:31:00 AM

0

Paul Mckibbin 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
--
Posted via http://www.ruby-....

David A. Black

3/21/2008 12:18:00 PM

0

Hi --

On Fri, 21 Mar 2008, Paul Mckibbin wrote:

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

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


David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.r... for details. Berlin dates coming soon!

Shandy Nantz

3/21/2008 2:41:00 PM

0

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


--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

3/22/2008 11:14:00 AM

0

On Fri, Mar 21, 2008 at 3:41 PM, Shandy Nantz <shandybleu@yahoo.com> 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.