Ruby Building Web Apps with Sinatra Finishing Touches Deleting Pages

Solace Enwere
Solace Enwere
15,102 Points

delete_content not deleting

I have this

delete "/:title" do
  delete_content(params[:title])
  redirect "/"
end

not sure why it's not deleting the txt page. I'm I missing something? It redirects me to Sinatra doesn't know this ditty page :(

3 Answers

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

OK. With the log and your full code, I was able to figure it out.

The first clue is this in the log:

"DELETE / HTTP/1.1"

It's sending a delete request to a path of /, rather than the correct path of /John%20Doe. Since there's no delete "/" route defined (only delete "/:title" is defined), you get a 404 (resource not found) response.

So why is it sending the delete request to / and not (e.g.) /John%20Doe? If you preview the app, bring up the "show" page, and right-click the delete button, you'll see this HTML code for the form:

<form method="post" action="/">

And why does does the action attribute have an incomplete path? Well, the code to set up that link is in show.erb:

<form method="post" action="/<% @title %>">

And there's the problem. <% @title %> evaluates to the page title, but then does nothing with it. To insert the value into the HTML, you need <%= @title %> instead. Fix that, and everything should work correctly.

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Here's a snapshot of a Workspace with a working delete route: https://w.trhou.se/hcj51gqayh

Your route code looks identical to mine, so I don't think that's the problem. Are you sure that it's not deleting the file? Maybe it is deleting the file, and it's the redirect to / that's failing.

When you click a delete link, the Sinatra log in your terminal should look something like this:

70.185.39.169 - - [05/Jun/2018:21:41:02 +0000] "DELETE /Nick%20Pettit HTTP/1.1"                           
 303 - 0.0262                                                                                             
10.120.36.5 - - [05/Jun/2018:21:41:02 UTC] "POST /Nick%20Pettit HTTP/1.1" 303 0                           
http://port-4567-qg8awkdmw1.treehouse-app.com/Nick%20Pettit -> /Nick%20Pettit                             
70.185.39.169 - - [05/Jun/2018:21:41:03 +0000] "GET / HTTP/1.1" 200 126 0.0200                            
10.120.36.5 - - [05/Jun/2018:21:41:03 UTC] "GET / HTTP/1.1" 200 126                                       
http://port-4567-qg8awkdmw1.treehouse-app.com/Nick%20Pettit -> /

Especially important are those 200 status codes, which mean "success". If you're getting a different status code on one of those requests, that's where your problem is.

If you're still having trouble, please post your full wiki.rb code (or a Workspace snapshot link), as well as a Sinatra log snippet showing what happens when you click a delete link.

Solace Enwere
Solace Enwere
15,102 Points

hmm... I'm still having trouble with it. But here's a Workspace snapshot link. https://w.trhou.se/d3srm44qpr

and this is what my terminal logs.

121.121.80.43 - - [06/Jun/2018:03:15:24 +0000] "DELETE / HTTP/1.1" 404 489 0.0176                                  
10.120.36.5 - - [06/Jun/2018:03:15:24 UTC] "POST / HTTP/1.1" 404 489                                               
http://port-4567-llatu5y2z4.treehouse-app.com/John%20Doe -> /                                                      
121.121.80.43 - - [06/Jun/2018:03:15:25 +0000] "GET /__sinatra__/404.png HTTP/1.1" 200 18893 0.0490                
10.120.36.5 - - [06/Jun/2018:03:15:24 UTC] "GET /__sinatra__/404.png HTTP/1.1" 200 18893                           
http://port-4567-llatu5y2z4.treehouse-app.com/ -> /__sinatra__/404.png                                             
121.121.80.43 - - [06/Jun/2018:03:15:25 +0000] "GET /favicon.ico HTTP/1.1" 200 243 0.0406                          
10.120.36.5 - - [06/Jun/2018:03:15:25 UTC] "GET /favicon.ico HTTP/1.1" 200 243                                     
http://port-4567-llatu5y2z4.treehouse-app.com/ -> /favicon.ico
Solace Enwere
Solace Enwere
15,102 Points

Ahh works now! Thanks for your help Jay!