Ruby on rails 模型-引用非模型表单字段/参数进行验证?

Ruby on rails 模型-引用非模型表单字段/参数进行验证?,ruby-on-rails,Ruby On Rails,轨道2.3.5 假设我只想验证sub\u report\u name是否选中了复选框sub\u report\u active。但是,sub\u report\u active不是模型的一部分。它是一个表单字段和一个参数,但它是一个check\u bok\u标记而不是一个模型字段 您是否可以引用模型中不是模型字段的参数/表单字段(如下所示……模型中无法识别我尝试过的任何形式的excpetsub\u report\u active) 我不知道您是否可以,但您可以使用attr_accessor轻松地

轨道2.3.5

假设我只想验证
sub\u report\u name
是否选中了复选框
sub\u report\u active
。但是,
sub\u report\u active
不是模型的一部分。它是一个表单字段和一个参数,但它是一个
check\u bok\u标记
而不是一个模型字段

您是否可以引用模型中不是模型字段的参数/表单字段(如下所示……模型中无法识别我尝试过的任何形式的excpet
sub\u report\u active


我不知道您是否可以,但您可以使用attr_accessor轻松地将其设置为模型字段,而无需相应的数据库列:

class YourModel < ActiveRecord::Base
  attr_accessor :sub_report_active
  validates_presence_of :sub_report_name, :if => proc{ |m| m.sub_report_active == 'YES' }
end
classyourmodelproc{m|m.sub_report_active=='YES'}
终止
这起到了作用(包括需要将激活的子报告从复选框标记更改为f复选框)

class Report < ActiveRecord::Base
  attr_accessor :sub_report_active

  validates_presence_of :sub_report_name, :if => :sub_report_active_yes?, :message => " must be entered if 'Sub Report Active?'' is checked."

  def sub_report_active_yes?
    sub_report_active == 'YES'
  end
End
类报告:sub_report_active_yes?、如果选中“sub report active?”,则必须输入消息=>
def sub_报告_激活_是吗?
子_报告_活动=='是'
终止
终止

谢谢-正在努力解决这个问题。Proc将返回“#”,而不是“是”或“否”(如果重要的话,我将使用Ruby 1.87/Rails 2.3.5)
class Report < ActiveRecord::Base
  attr_accessor :sub_report_active

  validates_presence_of :sub_report_name, :if => :sub_report_active_yes?, :message => " must be entered if 'Sub Report Active?'' is checked."

  def sub_report_active_yes?
    sub_report_active == 'YES'
  end
End