Sinatra controller delete method failing

343 Views Asked by At

I have a vendor model and controller where I've implemented the below delete method. Whenever I click the delete button, I get the "doesn't know this ditty" error.

  delete '/vendors/:id/delete' do
    @vendor = Vendor.find(params[:id])
    if logged_in? && @vendor.wedding.user == current_user
      @vendor.destroy
      redirect '/vendors'
    else
      redirect "/login", locals: {message: "Please log in to see that."}
    end
  end

My delete button:

<form action="/vendors/<%[email protected]%>/delete" method="post">
  <input id="hidden" type="hidden" name="_method" value="delete">
  <input type="submit" value="Delete Vendor">
</form>

My config.ru file already has 'use Rack::MethodOverride' and my edit/put forms are working fine so MethodOverride seems to be working.

Any idea why Sinatra is giving me the "Sinatra doesn't know this ditty" message just for deleting?

1

There are 1 best solutions below

0
Bobby Matson On BEST ANSWER

As suggested by Matt in the comments, you might want to try enabling the method override in your app via set. For example, using the modular Sinatra setup:

class Application < Base

  set :method_override, true

  # routes here
end

There's a nice example in this writeup as well