-2

如何從嵌套資源訪問ID?在@[email protected]中沒有id people_id。我得到一個錯誤Couldn't find People without an ID。提前感謝您。如何從嵌套資源獲取ids?

模型

class City < ActiveRecord::Base 
    has_many :peoples 
    end 

    class People < ActiveRecord::Base 
    belongs_to :city 
    end 

控制器

def show 
    @city = City.find(session[:city_id]) 
    @[email protected] 
    @[email protected](params[:id]) 
    end 

路線

resources :city, :people 
    resources :cities do 
    resources :peoples 
    end 

回答

1

你的錯誤信息是指模擬Deal,但你的例子是指City和兒童People。無論如何,@Peoples(或@city.peoples)中的每個People實例的ID屬性都應爲id - 而不是people_id

+0

對不起,這是複製粘貼錯字。但事情是它說不能找到沒有ID的人,這意味着在人的實例中沒有ID,這使我認爲這與路線有關 – katie

0

我覺得從這一行

@people = @peoples.find(params[:id]) 

你確定你設置params[:id]您的問題源於?你如何鏈接到你的表演動作?

另外我不認爲你可以調用實例變量@peoples查找,因爲我認爲它是一個數組,但我可能是錯的。