Reputation: 1254
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 '#{pallet_hist.id}' deleted successfully"
redirect_to(:action => 'index', :id => @pallet_hist.pallet_id)
end
end
I can't figure out why it's still giving me this error.
Routes.rb
Rails.application.routes.draw do
match ':controller(/:action(/:id))', :via => [:get, :post]
root 'pallets#index'
end
Upvotes: 0
Views: 7599
Reputation: 135
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.
EDIT:
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.
Upvotes: 1
Reputation: 1000
You need to add your destroy
action to your routes.
Add the following line: resources :pallet_hists
to your config/routes.rb
would solve the problem.This will add all routes (update
, index
, create
...).
You can use resources :pallet_hists, only: :destroy
if you only want to add destroy
route.
Upvotes: 0