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.