我正在练习在Sinatra中制作一个Web应用程序,我正在尝试更新一个页面,以便从一个数据库中获取信息后会显示一次按钮被点击(更具体地说,我是一个使用学生数据库的学生年龄,姓名和校园位置)。我有一个5个按钮(每个校园一个)的路线,一旦校园按钮被点击,我希望网页更新所有学生的信息(来自该校园)在同一页面上。如何在单击按钮时在同一页面上显示数据[Sinatra/Ruby]
这就是我对app.rb
require 'sinatra'
require 'sqlite3'
set :public_folder, File.dirname(__FILE__) + '/static'
db = SQLite3::Database.new("students.db")
db.results_as_hash = true
get '/' do
@students = db.execute("SELECT * FROM students")
erb :home
end
get '/students/new' do
erb :new_student
end
get '/students/campus' do
erb :campus
end
get '/students/campus/:campus' do
erb :campus
campus_data = db.execute("SELECT * FROM students WHERE campus=?", params[:campus])
campus_data.to_s
response = ""
campus_data.each do |student|
response << "ID: #{student['id']}<br>"
response << "Name: #{student['name']}<br>"
response << "Age: #{student['age']}<br>"
response << "Campus: #{student['campus']}<br><br>"
end
response
end
post '/students' do
db.execute("INSERT INTO students (name, campus, age) VALUES (?,?,?)", [params['name'], params['campus'], params['age'].to_i])
redirect '/'
end
这就是我对campus.erb
<!DOCTYPE html>
<html>
<head>
<title>Campus Cohorts</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Cohorts</h1>
<p>Click on a button to see all students from that location.</p>
<a href="/students/campus/SF"><button type="button">San Francisco</button></a>
<a href="/students/campus/NYC"><button type="button">New York City</button></a>
<a href="/students/campus/SD"><button type="button">San Diego</button></a>
<a href="/students/campus/CHI"><button type="button">Chicago</button></a>
<a href="/students/campus/SEA"><button type="button">Seattle</button></a>
</body>
</html>
目前,如果一个按钮被按下它会重定向你到一个新的页面,那个校园的学生信息表格,我怎样才能在同一页面上呈现信息?