레일즈 3에는 데이터베이스가 아닌 모든 모델에 유효성 검사를 추가하기위한 ActiveRecord 모듈이 포함됩니다. 양식 (예 : ContactForm 모델)에 대한 모델을 만들고 ActiveRecord 평가를 포함하고 싶습니다. 그러나 Rails 2.3.11에는 ActiveRecord 모듈을 포함시킬 수 없습니다. 레일즈 2.3.11에서 레일스 3와 동일한 동작을 수행 할 수있는 방법이 있습니까?레일 2.3.11 폼용 모델 생성 및 ActiveRecord 유효성 검사
3
A
답변
2
하나 이상의 모델에 대한 유효성 검사 프록시의 일종으로 가상 클래스를 사용하려는 경우 다음이 도움이 될 수 있습니다 (2.3.x의 경우 3.xx를 사용하면 앞에서 설명한 ActiveModel을 사용할 수 있음).
class Registration
attr_accessor :profile, :other_ar_model, :unencrypted_pass, :unencrypted_pass_confirmation, :new_email
attr_accessor :errors
def initialize(*args)
# Create an Errors object, which is required by validations and to use some view methods.
@errors = ActiveRecord::Errors.new(self)
end
def save
profile.save
other_ar_model.save
end
def save!
profile.save!
other_ar_model.save!
end
def new_record?
false
end
def update_attribute
end
include ActiveRecord::Validations
validates_format_of :new_email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_presence_of :unencrypted_pass
validates_confirmation_of :unencrypted_pass
end
이 방법 당신은 당신이 그들을 정의하기 전에 그것을 포함하려고하면 save
및 save!
방법을 사용할 수없는 것을 불평 유효성 검증 서브 모듈을 포함 할 수 있습니다. 아마도 최선의 해결책은 아니지만 작동합니다.
관련 문제
- 1. ActiveRecord 유효성 검사 예외 생성 (아니!)
- 2. 레일 정적 모델 유효성 검사
- 3. ActiveRecord - 모델 유효성 검사 오류를 경고로 바꿉니다.
- 4. 레일 모델 유효성 검사 - validates_inclusion_of
- 5. 모델 외부의 레일 유효성 검사
- 6. 생성 및 업데이트시 레일즈 모델 유효성 검사
- 7. 레일 서버 측 유효성 검사 - belongs_to 모델
- 8. Rails ContactForm이 아닌 ActiveRecord 모델 - 유효성 검사
- 9. 레일 모델 유효성 검사 : 날짜의 심볼
- 10. 연관된 모델의 데이터를 기반으로 한 유효성 검사 및 ActiveRecord 모델?
- 11. 여러 모델 양식으로 레일 유효성 검사
- 12. activerecord 유효성 검사 - validates_associated
- 13. ActiveRecord : 사용자 별 유효성 검사
- 14. 레일 유효성 검사 및 롤백
- 15. 레일 유효성 검사 오류
- 16. 모델 생성 중 유효성 검사 오류
- 17. 레일 모델 유효성 검사 건너 뛰기 및 순서
- 18. MVC 유효성 검사 모델
- 19. ActiveRecord 콜백 중 유효성 검사
- 20. 레일 유효성 검사 문제
- 21. MVC 모델 유효성 검사
- 22. 모델 유효성 검사
- 23. 복잡한 모델 유효성 검사
- 24. 레일 다형 모델. 보기에서 양식 유효성 검사 오류를 표시하는 방법?
- 25. 레일 모델 유효성 검사기
- 26. 레일 모델
- 27. Rails ActiveRecord : 연결 유효성 검사 건너 뛰기
- 28. 레일 유효성 검사 오류 처리
- 29. 레일 모달 폼 유효성 검사
- 30. FBML + jquery 유효성 검사 + 레일