显示父类 Rails 4.0 的子类 [英] Show children of a parent class Rails 4.0

查看:54
本文介绍了显示父类 Rails 4.0 的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个供学校和学生使用的模型.他们的关系是 School has_many Students.我试图呈现一个视图,如果我在学校索引页面上并单击显示",它将打开一个新页面并显示属于该学校的学生列表.例如:

I have a model for School and Student. Their relationship is School has_many Students. I am trying to render a view where if I am on the School index page and I click "Show", it will open up a new page and display a list of students that belong to that school. Ex:

School
- Student1
- Student2
- Student3

推荐答案

您可以执行以下操作:

# students controller
def index
  @students = Student.scoped
  @students = @students.where(school_id: params[:school_id]) if params[:school_id].present?
end

# view of the schools index
- @schools.each do |school|
  = link_to "students of the school #{school.id}", students_path(school_id: school.id)

如果您不完全理解,请不要犹豫寻求解释;)

Don't hesitate to ask for explanations if you don't fully understand ;)

这篇关于显示父类 Rails 4.0 的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆