Padagomez Padagomez - 1 year ago 117
Ruby Question

AbstractController::ActionNotFound - The action 'destroy' could not be found

I'm having some trouble with my rails application. I checked other questions of this error and none of them are applicable to my application.

I need to destroy a row, and to do that I direct a form to the action 'destroy' within my controller.

Here is where I direct it.

<%= form_for(:pallet_hist, :url => {:action => 'destroy', :id => params[:id]}) do |f| %>

Here's the action within the controller.

class PalletHistController < ApplicationController

def destroy
@pallet_hist = PalletHist.find(params[:id]).destroy
flash[:notice] = "Entry '#{}' deleted successfully"
redirect_to(:action => 'index', :id => @pallet_hist.pallet_id)

I can't figure out why it's still giving me this error.


Rails.application.routes.draw do

match ':controller(/:action(/:id))', :via => [:get, :post]

root 'pallets#index'

Answer Source

For a destroy action, you want to use the delete HTTP method within your routes. So for a custom route (if you're not using resources to generate a set of routes) it would be something like:

delete 'my_url', to: 'mycontroller#my_delete_action'

If you were to use resources it would give you index, show, new, create, edit, update, and destroy routes.

Also, I'd be surprised if your :url => {:action => 'destroy', :id => params[:id]} works. The url: option should contain the destroy action's url. So if you did that custom route I mentioned above, the url would be something closer to mycontroller_path and then there should be a method: :delete after it. I could be wrong on this last part, but thought I'd give the extra information in case I'm right.


If all you need is a delete button, then you probably don't want to create a form just for that. You'd be better off using a link. So that would be something like:

<%= link_to 'Delete', mycontroller_path(object_to_destroy), method: :delete %>

I hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download