Need help with a simple IF/ELSE statement?

I’m using hivelogic’s email enkoder and need help with a simple if/else
statement. If there is a value for participant.student.email I would
like to create the hyperlink for the email address, if not I would just
like to list their name with no link. Can someone tell me how to do
this? I can’t seem to get it to work or find any good information on
using nil with IF/ELSE statements.

[code]@name = “#{participant.student.last_name},
#{participant.student.first_name}”
@email = participant.student.email

if !@email.nil?
enkode_mail(@email, @name)
else
@name
end[/code]

Looks OK to me, but I don’t know how enkode_mail works.

Maybe you meant to assign the result of enkode_mail(…) to @email?

@email = enkode_mail(@email, @name)

On Sun, Oct 15, 2006, John S. wrote:

I’m using hivelogic’s email enkoder and need help with a simple if/else
statement. If there is a value for participant.student.email I would
like to create the hyperlink for the email address, if not I would just
like to list their name with no link. Can someone tell me how to do
this? I can’t seem to get it to work or find any good information on
using nil with IF/ELSE statements.

Try using #blank? instead of #nil?. In all likelihood, what’s happening
is you’re getting an empty string back from the database when it’s not
present.

Also, you might consider using unless instead of if !:

unless @email.blank?
enkode_mail @email, @name
else
@name
end

Ben

Thanks for the help!!

Hi –

On Sun, 15 Oct 2006, Ben B. wrote:

Try using #blank? instead of #nil?. In all likelihood, what’s happening
is you’re getting an empty string back from the database when it’s not
present.

Also, you might consider using unless instead of if !:

unless @email.blank?
enkode_mail @email, @name
else
@name
end

Or, if like me you prefer to avoid the somewhat awkward unless/else
sequence, you can switch the whole thing around:

if @email.blank?
@name

etc.

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org