Ruby script if statement

Hi,
I want to write a if statement script, what it will do is if the phone
number field exist, then print the phone number, otherwise leave it
blank.

I have tried this:

<% if @user.phone_work != 0 %>Work: <%= @user.phone_work -%>
<% else %>

<% end %>

it’s not work, anyone can advice please?

puts “Work: #{@user.phone_work}” if @user.phone_work !=0

will leave it out if work == 0, else it prints the line.

Is there something else you want to do maybe that I’m missing?

Wayne

Thanks Wayne

follow your instruction, I tried this:
<% “Work: #{@user.phone_work}” if @user.phone_work !=0 -%>

which is not work, the field will disappear not matter exist or not
any idea why?

what is @user.phone_work? a string?

Try this:

puts “Work: #{@user.phone_work}” if !@user.phone_work.nil?


From: yinwen Xuan [email protected]
To: [email protected]
Sent: Wednesday, November 20, 2013 3:54 PM
Subject: Re: ruby script if statement

Thanks Wayne

follow your instruction, I tried this:
<% “Work: #{@user.phone_work}” if @user.phone_work !=0 -%>

which is not work, the field will disappear not matter exist or not
any idea why?

On 2013-Nov-20, at 17:07 , yinwen Xuan [email protected] wrote:

HI thanks again Wayne!

I did tried
<% puts “Work: #{@user.phone_work}” if !@user.phone_work.nil? -%>

still the same, not showing at all no matter nil or not.

this field is for phone number.

Let me guess that you’re actually talking about a Ruby on Rails
application and don’t realize that there is a separate
mailing-list/forum for Rails questions.

If so, then you have ActiveSupport and the perfect method for this test:

@user.phone_work.blank? (or its opposite @user.phone_work.present?)

<% if @user.phone_work.present? -%>
Work: <%= @user.phone_work %>
<% end -%>

-Rob

-Rob

sorry for the poor Explanation and your code working good

Thanks!

HI thanks again Wayne!

I did tried
<% puts “Work: #{@user.phone_work}” if !@user.phone_work.nil? -%>

still the same, not showing at all no matter nil or not.

this field is for phone number.