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
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
FULL CONTROLLER
class AppointmentsController < ApplicationController
before_filter :load_clients_and_services, :only => [ :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 :ok }
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 :ok }
end
end
private
def load_clients_and_services
@clients = Client.find(:all)
@services = Service.find(:all)
end
end