我正在為我的應用程式添加促銷代碼選項。我想在表單上應用驗證,將用戶輸入的促銷代碼與管理儀表板中已經存在的促銷代碼相匹配(Activeadmin 用于管理儀表板)。
我嘗試了一些選項,但沒有任何效果。我試過的例子 -
def validate_promo_code
if promo_code.present? and (!promo_code.match(EngineName::PromoCode.promo_code))
errors.add :promo_code, "must be a valid promo code"
end
end
def validate_promo_code
if promo_code.present? and (promo_code != EngineName::PromoCode.where(promo_code: promo_code))
errors.add :promo_code, "must be valid promo code"
return;
end
end
有誰知道如何實作這一目標?請幫忙!
uj5u.com熱心網友回復:
您必須讓您的 activerecord 模型知道使用validatedsl 陳述句的驗證。這行得通嗎?
validate :ensure_valid_promo_code
def ensure_valid_promo_code
if promo_code.present? && (!promo_code.match(EngineName::PromoCode.promo_code))
errors.add :promo_code, "must be a valid promo code"
end
end
uj5u.com熱心網友回復:
如果有人仍在尋找解決方案,試試這個 -
def validate_promo_code
existing_code = EngineName::PromoCode.where(promo_code: promo_code)
if promo_code.present? and !(existing_code.present?)
errors.add :promo_code, "must be valid promo code"
end
end
這個解決方案對我有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/435519.html
