Ruby on rails 按班级对学生进行分类
我有一个学生模型,其中包含名称:string和教室:string列 我希望有一个页面列出每个班级的学生 i、 e.Ruby on rails 按班级对学生进行分类,ruby-on-rails,Ruby On Rails,我有一个学生模型,其中包含名称:string和教室:string列 我希望有一个页面列出每个班级的学生 i、 e.本地主机:3000/学生/1E1 本地主机:3000/学生/1E2 本地主机:3000/学生/1E3等 我似乎无法解决问题。这是我第一次尝试自己开发Rails应用程序。但我真的被卡住了!希望有人能帮我 此代码在我的学生\u controller.rb中 class StudentsController < ApplicationController def index
本地主机:3000/学生/1E1
本地主机:3000/学生/1E2
本地主机:3000/学生/1E3
等 我似乎无法解决问题。这是我第一次尝试自己开发Rails应用程序。但我真的被卡住了!希望有人能帮我 此代码在我的学生\u 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
<h1>Students#show</h1>
<p>Find me in app/views/students/show.html.erb</p>
<%= @student.name %>
<%= @student.classroom%>
class Student < ActiveRecord::Base
has_and_belongs_to_many :classrooms
end
class Classroom < ActiveRecord::Base
has_and_belongs_to_many :students
end
class Student < ActiveRecord::Base
belongs_to :classroom
end
class Classroom < ActiveRecord::Base
has_many :students
end
class ClassroomsController < ApplicationController
def index
@classrooms = Classroom.all
end
def show
@classroom = Classroom.find(params[:id])
end
end
<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>
<h1>Classrooms</h1>
<table>
<% @classrooms.each do |classroom| %>
<tr>
<td><%= classroom %></td>
<td><%= link_to(classroom, classroom_path(classroom)) %></td>
</tr>
<% end %>
</table>
<h1>Classroom <%= @classroom %> Students</h1>
<table>
<% @students.each do |student| %>
<tr>
<td><%= student.name %></td>
</tr>
<% end %>
</table>
class StudentsController
此代码位于my show.html.erb
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
<h1>Students#show</h1>
<p>Find me in app/views/students/show.html.erb</p>
<%= @student.name %>
<%= @student.classroom%>
class Student < ActiveRecord::Base
has_and_belongs_to_many :classrooms
end
class Classroom < ActiveRecord::Base
has_and_belongs_to_many :students
end
class Student < ActiveRecord::Base
belongs_to :classroom
end
class Classroom < ActiveRecord::Base
has_many :students
end
class ClassroomsController < ApplicationController
def index
@classrooms = Classroom.all
end
def show
@classroom = Classroom.find(params[:id])
end
end
<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>
<h1>Classrooms</h1>
<table>
<% @classrooms.each do |classroom| %>
<tr>
<td><%= classroom %></td>
<td><%= link_to(classroom, classroom_path(classroom)) %></td>
</tr>
<% end %>
</table>
<h1>Classroom <%= @classroom %> Students</h1>
<table>
<% @students.each do |student| %>
<tr>
<td><%= student.name %></td>
</tr>
<% end %>
</table>
学生秀
在app/views/students/show.html.erb中查找我
按类排序是另一种路径,还是要在索引中按类排序
假设教室是与学生的关联(属于:教室),并且在学生表中有一个教室id列,您可以执行类似于student.order(:教室id)的操作,最好将“教室”作为模型并与学生关联:
m-n学生和教室协会:
在终端中生成:
rails g model classroom number:string
rails g migration create_classrooms_student classroom:references student:references
rails g model classroom number:string
rails g migration add_classroom_id_to_students classroom:references
模型/student.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
<h1>Students#show</h1>
<p>Find me in app/views/students/show.html.erb</p>
<%= @student.name %>
<%= @student.classroom%>
class Student < ActiveRecord::Base
has_and_belongs_to_many :classrooms
end
class Classroom < ActiveRecord::Base
has_and_belongs_to_many :students
end
class Student < ActiveRecord::Base
belongs_to :classroom
end
class Classroom < ActiveRecord::Base
has_many :students
end
class ClassroomsController < ApplicationController
def index
@classrooms = Classroom.all
end
def show
@classroom = Classroom.find(params[:id])
end
end
<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>
<h1>Classrooms</h1>
<table>
<% @classrooms.each do |classroom| %>
<tr>
<td><%= classroom %></td>
<td><%= link_to(classroom, classroom_path(classroom)) %></td>
</tr>
<% end %>
</table>
<h1>Classroom <%= @classroom %> Students</h1>
<table>
<% @students.each do |student| %>
<tr>
<td><%= student.name %></td>
</tr>
<% end %>
</table>
模型/student.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
<h1>Students#show</h1>
<p>Find me in app/views/students/show.html.erb</p>
<%= @student.name %>
<%= @student.classroom%>
class Student < ActiveRecord::Base
has_and_belongs_to_many :classrooms
end
class Classroom < ActiveRecord::Base
has_and_belongs_to_many :students
end
class Student < ActiveRecord::Base
belongs_to :classroom
end
class Classroom < ActiveRecord::Base
has_many :students
end
class ClassroomsController < ApplicationController
def index
@classrooms = Classroom.all
end
def show
@classroom = Classroom.find(params[:id])
end
end
<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>
<h1>Classrooms</h1>
<table>
<% @classrooms.each do |classroom| %>
<tr>
<td><%= classroom %></td>
<td><%= link_to(classroom, classroom_path(classroom)) %></td>
</tr>
<% end %>
</table>
<h1>Classroom <%= @classroom %> Students</h1>
<table>
<% @students.each do |student| %>
<tr>
<td><%= student.name %></td>
</tr>
<% end %>
</table>
或者简单的方法:
为教室创建新控制器
class ClassroomsController < ApplicationController
def index
@classrooms = Student.group(:classroom).pluck(:classroom)
end
def show
@classroom = params[:id]
@students = Student.where(params[:id])
end
end
class ClassroomsController
index.html.erb
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
<h1>Students#show</h1>
<p>Find me in app/views/students/show.html.erb</p>
<%= @student.name %>
<%= @student.classroom%>
class Student < ActiveRecord::Base
has_and_belongs_to_many :classrooms
end
class Classroom < ActiveRecord::Base
has_and_belongs_to_many :students
end
class Student < ActiveRecord::Base
belongs_to :classroom
end
class Classroom < ActiveRecord::Base
has_many :students
end
class ClassroomsController < ApplicationController
def index
@classrooms = Classroom.all
end
def show
@classroom = Classroom.find(params[:id])
end
end
<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>
<h1>Classrooms</h1>
<table>
<% @classrooms.each do |classroom| %>
<tr>
<td><%= classroom %></td>
<td><%= link_to(classroom, classroom_path(classroom)) %></td>
</tr>
<% end %>
</table>
<h1>Classroom <%= @classroom %> Students</h1>
<table>
<% @students.each do |student| %>
<tr>
<td><%= student.name %></td>
</tr>
<% end %>
</table>
教室
show.html.erb
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
<h1>Students#show</h1>
<p>Find me in app/views/students/show.html.erb</p>
<%= @student.name %>
<%= @student.classroom%>
class Student < ActiveRecord::Base
has_and_belongs_to_many :classrooms
end
class Classroom < ActiveRecord::Base
has_and_belongs_to_many :students
end
class Student < ActiveRecord::Base
belongs_to :classroom
end
class Classroom < ActiveRecord::Base
has_many :students
end
class ClassroomsController < ApplicationController
def index
@classrooms = Classroom.all
end
def show
@classroom = Classroom.find(params[:id])
end
end
<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>
<h1>Classrooms</h1>
<table>
<% @classrooms.each do |classroom| %>
<tr>
<td><%= classroom %></td>
<td><%= link_to(classroom, classroom_path(classroom)) %></td>
</tr>
<% end %>
</table>
<h1>Classroom <%= @classroom %> Students</h1>
<table>
<% @students.each do |student| %>
<tr>
<td><%= student.name %></td>
</tr>
<% end %>
</table>
课堂学生
我只想要这样的页面……localhost:3000/students/1E1 localhost:3000/students/1E2 localhost:3000/students/1E3。那么:课堂应该是另一种模式吗?还是应该是学生属性之一?创建关系时使用:references
。它将自动在该字段上创建索引。谢谢大家。现在我明白了。我不认为课堂作为一种模式是完全有意义的。。。对于像你们这样善良慷慨的人来说,学习是伟大的。再次感谢。:)@Exsemt还删除了\u id
后缀,因为它现在不需要。