Ruby Building Web Apps with Sinatra Finishing Touches A DELETE Request

Tobias Jackson
Tobias Jackson
9,758 Points

Within the delete "/signatures/:index" route, call the delete_signature method. Pass the index URL parameter as the argu

Am I missing something?

views/show.erb
<p><%= @signature %></p>

<form method="post" action="/signatures/<%= @index %>">
  <input type="hidden" name="_method" value="delete">   
    <input type="hidden" name="_method" value="delete"> 
  <input type="submit" value="Delete This Signature"/>
</form>
guestbook.rb
require "sinatra"

def load_signature(index)
  # Code omitted for brevity
end

def save_signature(signature)
  # Code omitted for brevity
end

def update_signature(index, signature)
  # Code omitted for brevity
end

# Deletes the line at the given index and re-saves the file.
def delete_signature(index)
  lines = File.readlines("signatures.txt")
  index = index.to_i
  lines.delete_at(index)
  File.open("signatures.txt", "w") do |file|
    file.puts lines
  end
end

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

get "/signatures/:index" do
  @index = params[:index]
  @signature = load_signature(@index)
  erb 'show'.to_sym
end

delete "/signatures/" do
    delete_signature(params[:signatures])

end

2 Answers

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Your delete route needs to be defined like this:

delete "/signatures/:index" do
  # ...
end

If you do that, then within the route block, you can get the URL parameter using params[:index]. Pass that as an argument to the delete_signature method.

delete "/signatures/:index" do
   delete_signature(params[:index],  )
end

it worked thanks a lot!