【问题标题】:Validates presence of THIS or THAT验证 THIS 或 THAT 的存在
【发布时间】:2013-09-01 04:30:04
【问题描述】:

我有一个表单,提示用户输入标题和:this:that。用户不能同时输入这两个字段。

<% f.input :title%>
<% f.input :this %>
<% f.input :that%>

对于我的:title,我的模型中有

validates :title, :presence => true

如何通过either :this or :that 的验证

【问题讨论】:

    标签: ruby-on-rails validation ruby-on-rails-4


    【解决方案1】:

    你可以这样做

    validates :that, :presence => true, :if => Proc.new {this.blank?}
    validates :this, :presence => true, :if => Proc.new {that.blank?}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多