1
나는 여러 형태로 사용하는 다형성 주소 모델을 가지고 있습니다. 나는 그 모델에서 간단한 검증을하지만 작동하지 않는다. 내가 도대체 뭘 잘못하고있는 겁니까?유효성 확인 다형성 연관
주소 모델
class Address < ActiveRecord::Base
attr_accessible :street1, :street2, :city, state, :zip
belongs_to :addressable, :polymorphic => true
validates_length_of :state, :maximun => 2, :too_long => "Field State is to long"
validates_numericality_of :zip, :on => :create, :message => "Zip Code must be numeric!"
validates_length_of :zip, :within => 5..9, :too_long => "Zip Code to long.", :too_short => "Zip Code to short"
end
Location Model
class Location < ActiveRecord::Base
has_one :address, :as => :addressable, :dependent => :destroy
belongs_to :account
accepts_nested_attributes_for :address
validates_presence_of :name
validates_length_of :name, :maximum =>80
end
위치 편집 양식 - 어떤 도움 예를 들어이
<% form_for [@account, @location] do |f| %>
<table>
<%= render :partial => 'form', :locals => {:f => f} %>
</table>
<%= f.submit("Update") %>
<% end %>
감사합니다. Ed
오류가 없습니다. 만들거나 업데이트 할 때 유효성을 검사하지 않습니다. 예를 들어 우편 번호 필드에 문자를 입력 할 수 있으며 오류없이 레코드를 저장합니다. – user724120
주소 : has_one 관계에있는 위치 모델에 validate => true를 추가해보십시오. – iwiznia