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 Submitting the Edit Form Via PUT Request

Alek Cheverdak
Alek Cheverdak
6,148 Points

What's wrong? Help please

redirect URI.escape("/#{params["/signatures/new"]}") You need to pass a string of "/signatures/new" to the redirect method.

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

<form method="post" action="/signatures/<%= @index %>">
  <input type="hidden"  name="_method" value="put" >
  <input type="text" name="signature" value="<%= @signature %>">
  <input type="submit">
</form>
guestbook.rb
require "sinatra"


def load_signature(index)
  # Code omitted for brevity
end

# Updates the line at the given index and re-saves the file.
def update_signature(index, signature)
  lines = File.readlines("signatures.txt")
  index = index.to_i
  lines[index] = signature
  File.open("signatures.txt", "w") do |file|
    file.puts lines
  end
end

get "/:index/edit" do
  # Code omitted for brevity
end

get "/signatures/new" do
  # Code omitted for brevity
end

# ROUTE FOR PUT REQUESTS GOES HERE
put "/signatures/:index" do
  update_signature(params[:index], params["signature"])
  redirect URI.escape("/#{params["/signatures/new"]}")
end

2 Answers

Hi Alek,

The error message you get says "you need to pass a string of "/signatures/new" to the redirect." It means that literally. Just pass the string - not a param. This should work fine:

redirect URI.escape("/signatures/new")

Steve.

No problem! :+1: