我正在練習在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>
目前,如果一個按鈕被按下它會重定向你到一個新的頁面,那個校園的學生信息表格,我怎樣才能在同一頁面上呈現信息?