keith
keith

Reputation: 651

Having trouble using a if boolean statement to redirect in the controller

I may be going about this completely wrong

what I was wanting to do is something along the lines of the code below where when_appointment is a boolean.

def create
  @visit = Visit.create(visit_params)
  if visit.when_appointment == "true"
    redirect_to new_appointment_path
  else
    redirect_to new_chat_path, :notice => "User updated."
  end
end

Upvotes: 0

Views: 50

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

In ruby:

true == 'true'
# => false

and Rails type-cast attributes upon assignment, so if you have:

visit_params[:when_appointment]
# => "true"

or

visit_params[:when_appointment]
# => "1"

you would have:

visit = Visit.new(visit_params)
visit.when_appointment
# => true

So, returning to your question, you should have:

if visit.when_appointment
  # ...
else
  # ...
end

Upvotes: 2

Related Questions