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 trial

Ruby Building Web Apps with Sinatra Updating Data A Route for PUT Requests

Bad Request? Uri? I'm stuck. See note.

Getting this error message:

Bad Request

bad URI `/%3C%=%20@title%20%/edit%3E'. WEBrick/1.3.1 (Ruby/2.3.0/2015-12-25) at joels-macbook-pro.local:4567

I've looked at Ruby docs, stack overflow and multiple other places but can't figure out why I'm getting this error when I try to update wiki entries. Everything was great up until now but I'm totally stuck.

My post and put routes look like:

post "/create" do
  save_content(params["title"], params["content"])
  redirect URI.escape("/#{params["title"]}")
end

put "/:title" do
  save_content(params["title"], params["content"])
  redirect URI.escape("/#{params["title"]}")
end

which is exactly what I see in the video.

Also, my edit.erb looks like:

<h1>Edit Page: <%= @title %></h1>

<div>
  <form method="post" action="/<%= @title %>">
    <input type="hidden" name="_method" value="put">
    <fieldset>
      <label for="content">Content:</label>
      <textarea name="content" rows="10" cols="50" id="content"><%= @content %></textarea>
    </fieldset>
    <input type="submit">
  </form>
</div>

<a href="/">Back to Index</a>

Any ideas?

Joel

Jay McGavren
Jay McGavren
Treehouse Teacher

I don't see the full stack trace, but that error is almost certainly coming from your call to the redirect method. The code for your post and put routes looks good, so the problem isn't there... I bet params["title"] contains extra punctuation characters that aren't part of the title. Can you debug what that contains? (Probably by putting a puts params["title"] on the line before the call to redirect, submitting the form from your browser, and then checking the output in your terminal/console?)

2 Answers

Oliver Duncan
Oliver Duncan
16,642 Points

Just for fun, I took the URI from your error message and ran it through URI.unescape(). The resulting string is "/<%= @title %/edit>", which says to me at some point your forgot to close your eruby tag. If you already fixed this problem (as your erb file suggests), then your error message should be different now, because title is now (hopefully) getting evaluated before sending the request.

If it's not, could it possibly be that your (fixed) html isn't being updated? In any case, jump into the dev console in your browser and look at the form action attribute. If it's still action="/<%= @title %/edit", then maybe it's a browser cache issue. A hard refresh (cmd + shift + r on my mac) should fix that.

I ended up just logically looking through it and it didn't make sense to try to interpolate within a route. Fixed some "extra syntax" and everything ran. Man... coming from standard ruby... this is blowing my mind! I go to a software dev school in Denver but I needed this class to give me a more thorough understanding of what's happening. I appreciate these courses. Gives me more perspective.