Ruby Building Web Apps with Sinatra Adding New Data Submitting the Form Via POST Request

Christopher Kemp
Christopher Kemp
3,446 Points

"/signatures/new"

Not sure what i am missing here.

views/new.erb
<p>Please enter your name below.</p>

<form method="post" action="/signatures/create">
  <input type="text" name="signature">
  <input type="submit">
</form>

post "/signatures/create" do

end
guestbook.rb
require "sinatra"

# Appends the signature to the file as a new line.
def save_signature(signature)
  File.open("signatures.txt", "a") do |file|
    file.puts signature
  end
end

get "/signatures/new" do
  erb :new
end

post "/signatures/create" do
  save_signature(params[:signature])
  redirect URI.escape("#{"/signatures/new"}"
end

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Looks like you cleared three of the tasks with no problem, but got stuck on the fourth. I've copied your code below and added comments.

views/new.erb
<p>Please enter your name below.</p>

<form method="post" action="/signatures/create">
  <input type="text" name="signature">
  <input type="submit">
</form>

<!-- The below code isn't causing the challenge to fail, but it shouldn't be here. -->
post "/signatures/create" do

end
guestbook.rb
require "sinatra"

# Appends the signature to the file as a new line.
def save_signature(signature)
  File.open("signatures.txt", "a") do |file|
    file.puts signature
  end
end

get "/signatures/new" do
  erb :new
end

post "/signatures/create" do
  save_signature(params[:signature])
  # This line is the reason you were getting a "Try again" error - it's not compiling.
  # The most serious problem is that there's no closing ) at the end.
  # redirect URI.escape("#{"/signatures/new"}"

  # But there's a bunch of other stuff going on there that you don't need.
  # I'm actually a little surprised that it compiles even with a ), but it looks like
  # what you have there is equivalent to this line (which will also pass the challenge):
  redirect "/signatures/new"
  # "#{"/signatures/new"}" is the same as "/signatures/new". You shouldn't use "#{}"
  # string interpolation unless there is Ruby code or a variable that you want to include
  # in a string.
  # And you also don't need URI.escape, because there are no characters in
  # "/signatures/new" that are invalid for a URL.
end