Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 带有_选项的条件给出未定义的方法错误_Ruby On Rails_Ruby_Validation - Fatal编程技术网

Ruby on rails 带有_选项的条件给出未定义的方法错误

Ruby on rails 带有_选项的条件给出未定义的方法错误,ruby-on-rails,ruby,validation,Ruby On Rails,Ruby,Validation,我试图简化模型上的验证。 在尝试使用模型中的with_选项进行重构之前,我有: # model validates :number_of_locations, presence: true, if: -> { required_for_step?(:business_details) } def required_for_step?(step) return true if form_step.nil? return true if self.form_steps.ind

我试图简化模型上的验证。 在尝试使用模型中的with_选项进行重构之前,我有:

# model    
validates :number_of_locations, presence: true, if: -> { required_for_step?(:business_details) }

def required_for_step?(step)
  return true if form_step.nil?
  return true if self.form_steps.index(step.to_s) <= self.form_steps.index(form_step)
end
#模型
验证:位置的数量,状态:true,如果:->{u步骤所需的位置?(:业务详细信息)}
步骤(步骤)所需的def
如果form_step.nil返回true?
如果需要self.form_steps.index(step.to_s)_步骤(:business_details)do|step,则返回true|
步骤.验证:位置的数量,存在:真
结束
步骤(步骤)所需的def
如果form_step.nil返回true?

如果self.form_steps.index(step.to_s)无法从条件中删除
->{…}
部分,则返回true。它定义了一个lambda,在验证模型时调用并计算该lambda的一部分。如果没有lambda,则在加载类时代码会立即运行(以后不会再运行)

with_options:if=>->{required_for_step?(:business_details)}do| step|
步骤.验证:位置的数量,存在:真
结束
私有的
步骤(步骤)所需的def
形式_step.nil?| |
表格步骤索引(步骤至)
# model
with_options :if => required_for_step?(:business_details) do |step|
  step.validates :number_of_locations, presence: true
end

def required_for_step?(step)
  return true if form_step.nil?
  return true if self.form_steps.index(step.to_s) <= self.form_steps.index(form_step)
end
undefined method `required_for_step?' for #<Class:0x007f82c2919438>
with_options :if => -> { required_for_step?(:business_details) } do |step|
  step.validates :number_of_locations, presence: true
end

private
def required_for_step?(step)
  form_step.nil? || 
    form_steps.index(step.to_s) <= form_steps.index(form_step)
end