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
In the index action of your
QuizzesControlleryou need to add:The
@repositoryline 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
/quizzesbut it sounds like you have no desire for this. If this is the case, you can removeresources: quizzesfrom yourroutes.rb(the second one only, not the nested one).