如何在Ruby on Rails中从学生列表获取ID并跳转显示详情?
Hey there! Let's walk through setting up your student list and show page step by step—since you're new to Rails, I'll break this down clearly with code examples for each file you mentioned.
routes.rb) First, use Rails' RESTful resource routing to set up the necessary paths for your students. This will automatically generate the routes for the index (list) and show (single student) pages.
# config/routes.rb Rails.application.routes.draw do # Only generate the index and show routes to keep it clean resources :students, only: [:index, :show] # Add other routes here if needed end
You can verify the generated routes by running rails routes in your terminal—look for students_path (points to the list page) and student_path(:id) (points to a single student's detail page).
StudentsController) Your controller needs to fetch the data for the views. Add index and show actions to get all students and a single student respectively.
# app/controllers/students_controller.rb class StudentsController < ApplicationController def index # Fetch all students from the database to pass to the index view @students = Student.all end def show # Find the specific student using the ID from the URL parameters @student = Student.find(params[:id]) end end
The params[:id] comes from the URL (e.g., /students/1 will have params[:id] = 1), and Student.find retrieves the matching record from your database.
index.html.erb) Display all students as clickable links that point to their respective detail pages. Use Rails' link_to helper to generate the correct URLs automatically.
<!-- app/views/students/index.html.erb --> <h1>学生列表</h1> <ul> <% @students.each do |student| %> <li> <!-- Link to the student's show page, displaying their name --> <%= link_to student.name, student_path(student) %> <!-- Optional: Add more details like student number --> <!-- <%= link_to "#{student.name} (学号: #{student.student_number})", student_path(student) %> --> </li> <% end %> </ul>
When you pass the student object to student_path, Rails automatically uses its id to build the URL (e.g., /students/1 for the first student).
show.html.erb) Build a table to display the selected student's details using the @student variable from the controller.
<!-- app/views/students/show.html.erb --> <h1><%= @student.name %> 的详情</h1> <table border="1"> <tr> <th>姓名</th> <td><%= @student.name %></td> </tr> <tr> <th>学号</th> <td><%= @student.student_number %></td> </tr> <tr> <th>年龄</th> <td><%= @student.age %></td> </tr> <tr> <th>班级</th> <td><%= @student.classroom %></td> </tr> <!-- Add more rows for other fields in your Student model (e.g., email, address) --> </table> <!-- Add a link to go back to the student list --> <%= link_to "返回学生列表", students_path %>
Make sure the fields (like student_number, age) match the attributes defined in your Student model.
- Start your Rails server with
rails s - Visit
http://localhost:3000/studentsto see the list - Click any student's name—you should be taken to their detail page with the info table
- If you don't have any students in your database, open the Rails console (
rails c) and create test records:Student.create(name: "张三", student_number: "2023001", age: 18, classroom: "高一(1)班") Student.create(name: "李四", student_number: "2023002", age: 17, classroom: "高一(2)班")
内容的提问来源于stack exchange,提问作者user6232145

