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 Building Web Apps with Sinatra Updating Data Submitting the Edit Form Via PUT Request

Building Web Apps with Sinatra Challenge Task 5 of 6

Whats wrong?

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

<form method = "post" action ="/signatures/<%= @index %>"  >
  <input type="text" name="signature" value="<%= @signature %>">
  <input type="submit">
  <input type="hidden" name="_method" value="put">
</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


put "/signatures/0" do
  index(params["/signatures/0"]) 
  end
put "/signatures/:index" do
  update_signature(params["index"], params["signatures"])
end

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

Check again if the second parameter passed to update_signature should be called "signatures" (plural) or "signature" (singular). Also, note that when accessing values in the params hash, we usually use symbols, not strings, so [:index] instead of ["index"]. This should put you on the right track.

You need to switch from quotation marks to colons when you are indexing a hash.

put "/signatures/:index" do update_signature(params[:index], params[:signature]) end

Zarela Graves
Zarela Graves
9,920 Points
put "/signatures/:index" do
  update_signature(params[:index], params[:signature])
end