Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSolace Enwere
15,102 Pointsdelete_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
Treehouse TeacherOK. 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
Treehouse TeacherHere'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
15,102 Pointshmm... 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
15,102 PointsAhh works now! Thanks for your help Jay!