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

Francisco Palma
Francisco Palma
1,646 Points

Bad request when trying to edit a page: Bad URI `/%3C&=%20@title%20%%3E/edit'.

Hello,

So i have this problem with my code when i want to edit a page.

My code looks like this:

wiki.rb

require "sinatra"
require "uri"

set :bind, "0.0.0.0"

def page_content(title)
  File.read("pages/#{title}.txt")
rescue Errno::ENOENT
  return nil
end

def save_content(title, content)
  File.open("pages/#{title}.txt", "w") do |file|
    file.print(content)
  end
end

get "/" do
  erb :welcome
end

get "/new" do
  erb :new
end

get "/:title" do
  @title = params[:title]
  @content = page_content(@title)
  erb :show
end

get "/:title/edit" do
 @title = params[:title]
 @content = page_content(@title) 
 erb :edit
end

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

And my edit.rb looks like this (I jumped one step ahead in the course, that´s why it has some more syntax like input type. But i found out pretty quick that i have to solve my previous problem first).

<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 rows="10" columns="50" name="content" id="content"></textarea>
  </fieldset>
  <input type="submit">
</form>
  </div>

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

Any ideas? My code is identical to the teachers code in the video and i tried to do a hard refresh on my browser too. The edit page is one that is in the course (Nick Pettit)

I decoded the error message and got this: /<&= @title %>/edit

Is it my routing or my erb?

Thank you.