2014-09-23 2 views
0

레일 4.02를 사용합니다. (strong_params)중첩 된 리소스에서 정의되지 않은 메소드 has_one

3 모델 :

class Region < ActiveRecord::Base 
    has_many :cities 
end 

class City < ActiveRecord::Base 
    has_one :location 
end 

class Location < ActiveRecord::Base 
    belongs_to :city 
end 

보기 :

activerecord (4.0.3) lib/active_record/relation/delegation.rb:13:in each' activerecord 
(4.0.3) lib/active_record/relation/delegation.rb:13:in each' actionpack (4.0.3) 
lib/action_view/template.rb:143:in block in render' activesupport (4.0.3) 
lib/active_support/notifications.rb:161:in instrument' actionpack (4.0.3) 
lib/action_view/template.rb:141:in render' actionpack (4.0.3) 
lib/action_view/renderer/template_renderer.rb:49:in block (2 levels) in render_template' 
:
- @region.cities.each do |city| 
    %ol 
    %li 
     %h4.block-title-5 
     = link_to city.name, country_region_city_path(city.region.country, city.region, city) 
     %p 
     = city.city_h2 
     %p 
     = city.location.latitude 

내가 오류 메시지 undefined method "latitude"

이 전체 추적이다를 얻을

화 내가 여기서 잘못하고있는거야? 당신의 Location 모델은 정말 latitude 속성이있는 경우 감사

+0

한'위치/Location''테이블/model'는'latitude' 열/속성이 있습니다 –

+0

코드에 아무 것도없는 것 같습니다. 전체 오류 추적을 업데이트하십시오. 또한 도시에'belongs_to : region'을 추가하십시오. 당신이 위치 테이블에 city_id를 가지고 있어야하는 또 하나의 일 ' –

+0

예 위치 테이블은 속성 위도를 가지고 있습니다. 내가 로그 – Remco

답변

1

이 오류의 원인은 당신이 location에 대한 무 값이 데이터베이스의 일부 City 기록을 가지고있다. 당신이 전체 예외 메시지보고에 의한 경우인지 여부를 확인하실 수 있습니다, 그것은 당신이 당신의 City에 대한 필수 속성으로 당신이 latitude을 고려 여부에 따라 두 가지 선택이이 경우에?

NoMethodError: undefined method `latitude' for nil:NilClass 

같은 것입니다 모델 또는 아닙니다. 그 필요한 경우 다음 City 모델에 유효성 검사 규칙을 추가하여 해당 규칙을 적용 :

validates :location, presence: true 

아마도 데이터베이스 스키마에서 외부 키 컬럼에 NOT NULL 제약을.

없는 latitude 값은 당신이 당신의보기에서 누락 된 값을 처리 할 수있는 응용 프로그램에서 허용 또는 경우 :

= city.location.try(:latitude) 
+0

라이트 스테이크 ... 0 값이있는 레코드가 있습니다. 고마워요. 이제 작동합니다! – Remco

관련 문제