Skip to content

Commit

Permalink
Merge pull request #69 from anderskraken/95-templating
Browse files Browse the repository at this point in the history
Add delete student ERB template and route
  • Loading branch information
anderskraken authored Aug 1, 2016
2 parents fe34b02 + acc34ef commit 20a33af
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
17 changes: 16 additions & 1 deletion web_dev/sinatra_templating/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@
erb :new_student
end

get '/students/delete' do
@students = db.execute("SELECT * FROM students")
erb :delete_student
end


# create new students via
# a form
post '/students' do
db.execute("INSERT INTO students (name, campus, age) VALUES (?,?,?)", [params['name'], params['campus'], params['age'].to_i])
redirect '/'
end

# add static resources
# add static resources

# create a Route for delete_student

get '/delete-student' do ## seems to work the same way with GET or POST
db.execute("DELETE FROM students WHERE id = ?", [params['id']])
redirect '/'
end


Binary file modified web_dev/sinatra_templating/students.db
Binary file not shown.
21 changes: 21 additions & 0 deletions web_dev/sinatra_templating/views/delete_student.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>Delete a Student</title>
</head>
<body>
<h1>Delete a Student</h1>
<p>Enter the id number of the student you want to delete.</p>
<form action="/delete-student" method="GET"> <!-- seems to work the same way with GET or POST -->
<input type="text" name="id" placeholder="Id">
<button type="submit">Delete</button>
</form>

<ul>
<% @students.each do |student| %>
<li>#<%= student['id'] %> - <%= student['name'] %>, <%= student['age'] %></li>
<% end %>
</ul>

</body>
</html>
2 changes: 1 addition & 1 deletion web_dev/sinatra_templating/views/home.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<body>
<h1>Student Management System</h1>
<p>Welcome to the student management system.</p>
<p>Click <a href="students/new">here</a> to create a new student.</p>
<p>Click <a href="students/new">here</a> to create a new student, or <a href="students/delete">here</a> to delete a student.</p>
<ul>
<% @students.each do |student| %>
<li><%= student['name'] %>, <%= student['age'] %></li>
Expand Down

0 comments on commit 20a33af

Please sign in to comment.