【发布时间】:2014-05-10 04:25:47
【问题描述】:
我有一个由方法提交调用的 Rails 4.0.2 控制器操作。在此方法中,我检查特定字段以匹配条件,如果不匹配,我想向用户显示错误并再次调用 render new,而无需在方法中进行任何进一步的操作。于是我写了:
discount_coupon = DiscountCoupon.where(code: discount_code).first
if discount_coupon.nil? || (discount_coupon.present? && !discount_coupon.currently_active?)
flash[:alert] = t("flash.entries.create.invalid_coupon_code")
render :new
return
end
@entry.save
因此,在我的应用程序中,当 discount_coupon 变量错误并且程序进入条件时,它应该在 return 上停止执行,但不知何故它没有,并且调用了 @entry.save 方法并搞砸了整个东西。
我做错了什么?感谢您的帮助!
【问题讨论】:
-
该代码在我看来是正确的。尝试将您的条件更改为“如果为真”以确保您正在执行返回。
-
使用
render :new and return
标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord