我使用rails 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
查看:
- @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"
這是全跟蹤:
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'
Wha我在這裏做錯了嗎?由於
做'位置/ Location''表/ model'有任何'latitude'列/屬性 – 2014-09-23 08:07:55
你的代碼沒有什麼不對。請更新完整的錯誤跟蹤。在城市中添加「belongs_to:region」。還有一件事你應該在位置表中使用city_id以使其工作' – 2014-09-23 08:09:43
是位置表具有屬性寬容度。我更新了日誌 – Remco 2014-09-23 08:35:44