在使用if boolean语句在控制器中重定向时遇到问题

时间:2014-11-14 08:05:34

标签: ruby-on-rails ruby redirect ruby-on-rails-4 boolean

我可能会对此完全错误

我想要做的是下面的代码,其中when_appointment是一个布尔值。

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

1 个答案:

答案 0 :(得分:2)

在红宝石中:

true == 'true'
# => false
分配后,

和Rails类型转换属性,所以如果你有:

visit_params[:when_appointment]
# => "true"

visit_params[:when_appointment]
# => "1"
你会得到:

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

所以,回到你的问题,你应该:

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