Ruby on rails 显示所有记录而不是实例记录的嵌套路由
以下是相关模型:Ruby on rails 显示所有记录而不是实例记录的嵌套路由,ruby-on-rails,ruby-on-rails-3,routes,nested-attributes,Ruby On Rails,Ruby On Rails 3,Routes,Nested Attributes,以下是相关模型: 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 :repos
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
当我尝试此配置时,我会得到数据库中的所有测验,而不仅仅是我尝试转到/repositories/1/quizzes时指定ID的测验
你知道为什么吗?非常感谢您在
QuizzesController
的索引操作中花费大量时间,您需要添加:
def index
@repository = Repository.find(params[:repository_id])
@quizzes = @repository.quizzes
end
@repository
行将根据URL中的参数查找存储库。然后,它将找到基于该存储库的所有测验
然后在您的视图中,当您显示这些测验时,您可以循环浏览所有这些测验
注意
按照您当前设置路线的方式,您可以访问
/quizzes
页面,但听起来您似乎并不希望这样。如果是这种情况,您可以从routes.rb中删除资源:测验
(仅第二个,不是嵌套的)。谢谢,这就成功了。然而,你的建议让我意识到我做错了。。。再次感谢!很高兴我能帮忙。这是一个学习的过程。可能想看看Rails for Zombies at,当我刚开始的时候,我发现它非常有用。谢谢,我现在正在查看它!
def index
@repository = Repository.find(params[:repository_id])
@quizzes = @repository.quizzes
end