0
我在軌中有一些嵌套的對象。用戶 - > has_many:任務 - > has_one:位置。如何獲取子對象的值
昨天,我以爲我在將位置值鏈接到任務時遇到了問題,但現在我意識到我無法在顯示中輸出值。
我可以通過調試得到輸出
<%= for task in @user.tasks %> <%= debug task.locations %> <% end %>
輸出
--- !ruby/object:Location attributes: id: "1" address: "testing address" city: "chicago" attributes_cache: {} changed_attributes: {} etc. etc. etc.
所以我想,如果我用
<%= task.locations.address %>
的Rails會給我的地址字段。但我得到一個
undefined method 'address' for nil:NilClass
對我有什麼錯誤的任何建議嗎?
----------更新,包括模型---------------- 我的任務模型&位置是
class Task < ActiveRecord::Base attr_accessible :user_id, :date, :description, :location_id belongs_to :user has_one :location end class Location < ActiveRecord::Base attr_accessible :address, :city, :state, :zip has_many :tasks end
我看到你在看什麼,並更新了問題以顯示我的模型。不幸的是,這並不能解決問題。有沒有一種方法可以顯示嵌套模型是否可用,以及導軌期望的是什麼:位置或位置? – pedalpete 2011-02-16 23:47:41