If the variable is set

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

On Tue, Oct 12, 2010 at 11:57 AM, Leonel .
[email protected]wrote:

   end

GET /appointments

@past_appointments = Appointment.all(:order => ‘start’, :conditions
def show
def new
@appointment = Appointment.find(params[:id])
# send email only if a client email is set
format.xml { render :xml => @appointment.errors, :status =>
respond_to do |format|
end
end

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

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.

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.

@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?

On Tue, Oct 12, 2010 at 1:50 PM, Leonel . [email protected]
wrote:

@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

I was supposed to check for empty? because it’s an empty string.

The answer is in the moved post: http://www.ruby-forum.com/topic/221968

Thanks Rick, that list will also help me in the future :smiley:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs