Search code examples
rubysinatrasinatra-activerecord

Sinatra controller delete method failing


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/<%=@vendor.id%>/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?


Solution

  • 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