모델에서 유효성 검사를 단순화하려고합니다.with_options 조건에서 정의되지 않은 메서드 오류가 발생했습니다.
# 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
이 완벽하게 작동는 required_for_step에 양식 단계를 통과 : 시도하기 전에 내가 가진 모델에 with_options를 사용하여 리팩토링? 함수를 호출하고 사용자가있는 양식의 단계에 따라 값을 반환합니다. 그건 내가 '단계'에 올바르게 접근한다는 것을 의미합니다.
조건부로 유효성을 검사하기 위해이 모델에 대해 약 30 개의 필드가 있습니다 (난 혼란을 없애기 위해 여기에 하나만 표시했으나 with_options를 사용하면 훨씬 더 체계적으로 정리할 수 있고 조건문을 리팩토링 할 수 있습니다). .이 반환
# 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>
감사합니다. – Questifer