Ruby Building Web Apps with Sinatra Finishing Touches A DELETE Request

MaryAnn Eleanya
MaryAnn Eleanya
8,612 Points

I don't understand what I am doing wrong.

It says that I need to call the delete method but I thought it was already being called.

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

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

def delete "/signatures/:index" do
params[:index]
end
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 ROUTE GOES HERE

1 Answer

Miguel de Luis Espinosa
Miguel de Luis Espinosa
38,920 Points

Buna ziua, you may want to define a route for the delete action and, inside that route call delete_signature