if the variable is set.

L

Leonel *.*

How can I create an if statement to check if a variable contains a
value? If it does contain a value, I want to send an email. I'm doing
the following but it doesn't work...

--------------------------------------------------------
EXTRACT
--------------------------------------------------------
# send email only if a client email is set
if defined?(appointment.client.email)
Notifier.appointment_booked(@appointment).deliver
end
--------------------------------------------------------


--------------------------------------------------------
FULL CONTROLLER
--------------------------------------------------------
class AppointmentsController < ApplicationController
before_filter :load_clients_and_services, :eek:nly => [ :new, :create,
:edit ]

# GET /appointments
# GET /appointments.xml
def index
@appointments = Appointment.all:)order => 'start', :conditions => [
"start >= ?", Date.today ] )
appointments = Appointment.all:)order => 'start', :conditions => [
"start >= ?", Date.today ] )
appointments.group_by do |appointment|
appointment.start.strftime("%Y%m%d")
end

@past_appointments = Appointment.all:)order => 'start', :conditions
=> [ "start < ?", Date.today ] )

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @appointments }
end
end

# GET /appointments/1
# GET /appointments/1.xml
def show
@appointment = Appointment.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @appointment }
end
end

# GET /appointments/new
# GET /appointments/new.xml
def new
@appointment = Appointment.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @appointment }
end
end

# GET /appointments/1/edit
def edit
@appointment = Appointment.find(params[:id])
end

# POST /appointments
# POST /appointments.xml
def create
@appointment = Appointment.new(params[:appointment])

respond_to do |format|
if @appointment.save

# send email only if a client email is set
if defined?(appointment.client.email)
Notifier.appointment_booked(@appointment).deliver
end

format.html { redirect_to(@appointment, :notice => 'Appointment
was successfully created.') }
format.xml { render :xml => @appointment, :status => :created,
:location => @appointment }
else
format.html { render :action => "new" }
format.xml { render :xml => @appointment.errors, :status =>
:unprocessable_entity }
end
end
end

# PUT /appointments/1
# PUT /appointments/1.xml
def update
@appointment = Appointment.find(params[:id])

respond_to do |format|
if @appointment.update_attributes(params[:appointment])
format.html { redirect_to(@appointment, :notice => 'Appointment
was successfully updated.') }
format.xml { head :eek:k }
else
format.html { render :action => "edit" }
format.xml { render :xml => @appointment.errors, :status =>
:unprocessable_entity }
end
end
end

# DELETE /appointments/1
# DELETE /appointments/1.xml
def destroy
@appointment = Appointment.find(params[:id])
@appointment.destroy

respond_to do |format|
format.html { redirect_to(appointments_url) }
format.xml { head :eek:k }
end
end

private
def load_clients_and_services
@clients = Client.find:)all)
@services = Service.find:)all)
end

end
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

How can I create an if statement to check if a variable contains a
value? If it does contain a value, I want to send an email. I'm doing
the following but it doesn't work...

--------------------------------------------------------
EXTRACT
--------------------------------------------------------
# send email only if a client email is set
if defined?(appointment.client.email)
Notifier.appointment_booked(@appointment).deliver
end
--------------------------------------------------------


--------------------------------------------------------
FULL CONTROLLER
--------------------------------------------------------
class AppointmentsController < ApplicationController
before_filter :load_clients_and_services, :eek:nly => [ :new, :create,
:edit ]

# GET /appointments
# GET /appointments.xml
def index
@appointments = Appointment.all:)order => 'start', :conditions => [
"start >= ?", Date.today ] )
appointments = Appointment.all:)order => 'start', :conditions => [
"start >= ?", Date.today ] )
appointments.group_by do |appointment|
appointment.start.strftime("%Y%m%d")
end

@past_appointments = Appointment.all:)order => 'start', :conditions
=> [ "start < ?", Date.today ] )

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @appointments }
end
end

# GET /appointments/1
# GET /appointments/1.xml
def show
@appointment = Appointment.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @appointment }
end
end

# GET /appointments/new
# GET /appointments/new.xml
def new
@appointment = Appointment.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @appointment }
end
end

# GET /appointments/1/edit
def edit
@appointment = Appointment.find(params[:id])
end

# POST /appointments
# POST /appointments.xml
def create
@appointment = Appointment.new(params[:appointment])

respond_to do |format|
if @appointment.save

# send email only if a client email is set
if defined?(appointment.client.email)
Notifier.appointment_booked(@appointment).deliver
end

format.html { redirect_to(@appointment, :notice => 'Appointment
was successfully created.') }
format.xml { render :xml => @appointment, :status => :created,
:location => @appointment }
else
format.html { render :action => "new" }
format.xml { render :xml => @appointment.errors, :status =>
:unprocessable_entity }
end
end
end

# PUT /appointments/1
# PUT /appointments/1.xml
def update
@appointment = Appointment.find(params[:id])

respond_to do |format|
if @appointment.update_attributes(params[:appointment])
format.html { redirect_to(@appointment, :notice => 'Appointment
was successfully updated.') }
format.xml { head :eek:k }
else
format.html { render :action => "edit" }
format.xml { render :xml => @appointment.errors, :status =>
:unprocessable_entity }
end
end
end

# DELETE /appointments/1
# DELETE /appointments/1.xml
def destroy
@appointment = Appointment.find(params[:id])
@appointment.destroy

respond_to do |format|
format.html { redirect_to(appointments_url) }
format.xml { head :eek:k }
end
end

private
def load_clients_and_services
@clients = Client.find:)all)
@services = Service.find:)all)
end

end

Hi, Leonel, this forum is for Ruby, the programming language. It looks like
your question is about Rails, the web framework. Since you posted from
ruby-forum, you should be able to the ruby on rails forum by selecting the
next forum down on the main list. You'll want to repost your question there.
 
A

Alex Stahl

Looks like you're checking whether or not the variable itself exists, as
opposed to if it contains a value.

Try this instead:

if appointment.client.email
#do stuff & things
end

In this instance, if 'email == nil', the statement will evaluate to
false.

How can I create an if statement to check if a variable contains a
value? If it does contain a value, I want to send an email. I'm doing
the following but it doesn't work...

--------------------------------------------------------
EXTRACT
--------------------------------------------------------
# send email only if a client email is set
if defined?(appointment.client.email)
Notifier.appointment_booked(@appointment).deliver
end
--------------------------------------------------------


--------------------------------------------------------
FULL CONTROLLER
--------------------------------------------------------
class AppointmentsController < ApplicationController
before_filter :load_clients_and_services, :eek:nly => [ :new, :create,
:edit ]

# GET /appointments
# GET /appointments.xml
def index
@appointments = Appointment.all:)order => 'start', :conditions => [
"start >= ?", Date.today ] )
appointments = Appointment.all:)order => 'start', :conditions => [
"start >= ?", Date.today ] )
appointments.group_by do |appointment|
appointment.start.strftime("%Y%m%d")
end

@past_appointments = Appointment.all:)order => 'start', :conditions
=> [ "start < ?", Date.today ] )

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @appointments }
end
end

# GET /appointments/1
# GET /appointments/1.xml
def show
@appointment = Appointment.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @appointment }
end
end

# GET /appointments/new
# GET /appointments/new.xml
def new
@appointment = Appointment.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @appointment }
end
end

# GET /appointments/1/edit
def edit
@appointment = Appointment.find(params[:id])
end

# POST /appointments
# POST /appointments.xml
def create
@appointment = Appointment.new(params[:appointment])

respond_to do |format|
if @appointment.save

# send email only if a client email is set
if defined?(appointment.client.email)
Notifier.appointment_booked(@appointment).deliver
end

format.html { redirect_to(@appointment, :notice => 'Appointment
was successfully created.') }
format.xml { render :xml => @appointment, :status => :created,
:location => @appointment }
else
format.html { render :action => "new" }
format.xml { render :xml => @appointment.errors, :status =>
:unprocessable_entity }
end
end
end

# PUT /appointments/1
# PUT /appointments/1.xml
def update
@appointment = Appointment.find(params[:id])

respond_to do |format|
if @appointment.update_attributes(params[:appointment])
format.html { redirect_to(@appointment, :notice => 'Appointment
was successfully updated.') }
format.xml { head :eek:k }
else
format.html { render :action => "edit" }
format.xml { render :xml => @appointment.errors, :status =>
:unprocessable_entity }
end
end
end

# DELETE /appointments/1
# DELETE /appointments/1.xml
def destroy
@appointment = Appointment.find(params[:id])
@appointment.destroy

respond_to do |format|
format.html { redirect_to(appointments_url) }
format.xml { head :eek:k }
end
end

private
def load_clients_and_services
@clients = Client.find:)all)
@services = Service.find:)all)
end

end
 
L

Leonel *.*

@Josh: I'm sorry I thought since all I need to create is a Ruby if
statement it would fall under the Ruby forum. It's true I'm using it in
a Rails context but it basically is just a Ruby question. But if you
still want me to move it I will.

@Alex: could you please help me in the Ruby on Rails forum?
 
R

Rick DeNatale

@Josh: I'm sorry I thought since all I need to create is a Ruby if
statement it would fall under the Ruby forum. It's true I'm using it in
a Rails context but it basically is just a Ruby question. But if you
still want me to move it I will.

@Alex: could you please help me in the Ruby on Rails forum?

No, this is clearly a Ruby question, albeit in the context of Rails.
Some folks here are just a llttle too eager to pull there "this is the
Ruby not the Rails" forum trigger.


a = []
defined? a # => "local-variable"
defined? a.last # => "method"
defined? a.last.foo # => nil
a.last.nil? # => true
defined? a.last.nil? # => "method"

So
if defined?(appointment.client.email)

returns "method" if appointment refers to an object with a client
method which returns an object with an email method.

It doesn't send that last message.

Normally in Ruby if you would simply check that the email value was
non-nil and idiomatically this would just be

if appointment.client.email
#....
end

Because nil and false are the only non-truthy values in Ruby and it
would be unusual to set the email to false.


--
Rick DeNatale

Help fund my talk at Ruby Conf 2010:http://pledgie.com/campaigns/13677
Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top