【问题标题】:Sorting Students by Class按班级对学生进行排序
【发布时间】:2014-10-20 00:06:58
【问题描述】:

我有一个 Student 模型,其中包含 name:stringclassroom:string 列。

我希望有一个页面列出每个班级的学生。


localhost:3000/students/1E1
本地主机:3000/学生/1E2
本地主机:3000/students/1E3

我似乎无法解决问题。这是我尝试自己开发的第一个 Rails 应用程序。但我真的被困住了!希望有人可以帮助我。

这段代码在我的students_controller.rb中

class StudentsController < ApplicationController
  def index
    @students = Student.all
  end

  def show
    @student = Student.find(params[:id])
  end


  def sort_by_class
    @student = Student.find(params[:id])
    @class = @student.classroom
  end

end

此代码在我的 show.html.erb 中

<h1>Students#show</h1>
<p>Find me in app/views/students/show.html.erb</p>


<%= @student.name %>
<%= @student.classroom%>

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    最好将“教室”作为模型并关联到学生:

    m-n 学生和课堂协会:The has_and_belongs_to_many Association

    在终端生成:

    rails g model classroom number:string
    rails g migration create_classrooms_student classroom:references student:references
    

    models/student.rb

    class Student < ActiveRecord::Base
    
      has_and_belongs_to_many :classrooms
    
    end
    

    models/classroom.rb

    class Classroom < ActiveRecord::Base
    
      has_and_belongs_to_many :students
    
    end
    

    OR 1-n 学生和课堂协会:

    在终端生成:

    rails g model classroom number:string
    rails g migration add_classroom_id_to_students classroom:references
    

    models/student.rb

    class Student < ActiveRecord::Base
    
      belongs_to :classroom
    
    end
    

    models/classroom.rb

    class Classroom < ActiveRecord::Base
    
      has_many :students
    
    end
    

    两个例子的控制器

    controllers/classrooms_controller.rb

    class ClassroomsController < ApplicationController
      def index
        @classrooms = Classroom.all
      end
    
      def show
        @classroom = Classroom.find(params[:id])
      end
    end
    

    show.html.erb

    <h1><%= @classroom.number %></h1>
    
    <table>
      <thead>
      <tr>
        <th> Name </th>
      </tr>
      </thead>
      <tbody>
        <% @classroom.students.each do |student| %>
          <tr>
            <td><%= student.name %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
    

    您的网址:

    localhost:3000/classrooms/1E1
    

    【讨论】:

    • 创建关系时使用&lt;field&gt;:references。它将自动在该字段上创建索引。
    • 谢谢你们。现在我懂了。不认为课堂作为一个模型是完全有意义的。 ... 和像你这样善良和慷慨的人一起学习是很棒的。再次感谢你。 :)
    • @Exsemt 也删除了 _id 后缀,因为它现在已取消。
    【解决方案2】:

    sort_by_class 是另一条路线还是要在索引中按类别排序?

    假设教室是与学生(belongs_to :classroom)的关联,并且您在 Student 表中有一个 classroom_id 列,您可以执行 Student.order(:classroom_id) 之类的操作

    【讨论】:

    • 我只想拥有这样的页面...localhost:3000/students/1E1 localhost:3000/students/1E2 localhost:3000/students/1E3。那么 :classroom 应该是另一种模式吗?还是应该是 Student 属性之一?
    【解决方案3】:

    或者简单的方法:

    为教室创建新控制器

    class ClassroomsController < ApplicationController
      def index
        @classrooms = Student.group(:classroom).pluck(:classroom)
      end
    
      def show
        @classroom = params[:id]
        @students = Student.where(params[:id])
      end
    end
    

    index.html.erb

    <h1>Classrooms</h1>
    <table>
      <% @classrooms.each do |classroom| %>
        <tr>
          <td><%= classroom %></td>
          <td><%= link_to(classroom, classroom_path(classroom)) %></td>
        </tr>
      <% end %>
    </table>
    

    show.html.erb

    <h1>Classroom <%= @classroom %> Students</h1>
    <table>
      <% @students.each do |student| %>
        <tr>
          <td><%= student.name %></td>
        </tr>
      <% end %>
    </table>
    

    【讨论】:

      猜你喜欢
      • 2016-07-13
      • 2021-12-15
      • 2017-01-25
      • 1970-01-01
      • 2023-04-08
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多