Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Building Web Apps with Sinatra Finishing Touches Deleting Pages

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.

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

Ahh works now! Thanks for your help Jay!