Nested Routes Showing All Records Instead of Instance Records
nested-attributes, routes, ruby-on-rails, ruby-on-rails-3
Solution
In the index action of your `QuizzesController` you need to add:
def index
@repository = Repository.find(params[:repository_id])
@quizzes = @repository.quizzes
end
The `@repository` line will find the repository based on the parameter in your URL. Then it will find all of the quizzes based on that repository.
Then in your view, you can loop through all of those quizzes when you display them.
Note
The way you currently have your routes setup, you have the ability to access a page at `/quizzes` but it sounds like you have no desire for this. If this is the case, you can remove `resources: quizzes` from your `routes.rb` (the second one only, not the nested one).
Problem
These are the relevant models: ``` class Repository < ActiveRecord::Base has_many :quizzes, :dependent => :destroy has_one :key, :dependent => :destroy accepts_nested_attributes_for :key, :quizzes end class Quiz < ActiveRecord::Base belongs_to :repository has_many :topics, :dependent => :destroy accepts_nested_attributes_for :topics end ``` This is in my routes: ``` GqAPI::Application.routes.draw do resources :repositories do resources :quizzes end resources :quizzes resources :keys resources :topics resources :questions end ``` When I try this configuration I get all the quizzes in the database, not just the ones for the ID I specify when I try to go to /repositories/1/quizzes Any ideas on why? Thanks so much for your time