How to get ids from a nested resource?
How do i access ids from a nested resource ?There is no id people_id in@peoples=@city.peoples.i get an error Couldn't find People without an ID.Thank you in advance.
Models
class City < ActiveRecord::Base
has_many :peoples
end
class People < ActiveRecord::Base
belongs_to :city
end
controller
def show
@city = City.find(session[:city_id])
@peoples=@city.peoples
@people=@peoples.find(params[:id])
end
Routes
resources :city, :people
resources开发者_运维百科 :cities do
resources :peoples
end
Your error message refers to model Deal, but your example refers to City and children People. Anyhow, each People instance in @Peoples (or @city.peoples) should have its ID attribute available as id -- not people_id.
I think your problem stems from this line
@people = @peoples.find(params[:id])
Are you sure you set params[:id]? How are you linking to your show action?
Also I don't think you can call find on the instance variable @peoples as I think it is an array, but I could be wrong.
加载中,请稍侯......
精彩评论