如果单选按钮值为“some_string”,Rails 条件验证

问题描述 投票:0回答:1

如果单选

marketing_source_other
的值设置为
marketing_source
,我想验证字段
other

如果用户从

marketing_source
字段中选择其他,它将为用户提供一个文本输入,以输入他们如何找到站点。

我怎样才能在 rails 中进行这样的验证:

  validates_presence_of :marketing_source_other :if [marketing_source == "other"]

:if
部分应该是什么?

ruby-on-rails validation radio-button
1个回答
6
投票
  validates_presence_of :marketing_source_other, :if => lambda {|obj| obj.marketing_source == "other"}
© www.soinside.com 2019 - 2024. All rights reserved.