destroy
action must be called on HTTP DELETE
request and never on GET
. You should definitely fix the following code from your routes:
resources products do ## missing : here, it should be :products
## member block not required
member do
get :destroy ## destroy should never be GET request
end
end
You just need to define your routes as:
resources :products ## Notice :products and not products
This will generate following routes for you which you can check by running command rake routes
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PATCH /products/:id(.:format) products#update
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
Notice the last route generated.
destroy
action should be called as HTTP Delete
method which is why on your link you need to specify method: :delete
as
<% glyph_to "Delete", product, method: :delete, data: (confirm: "Are you sure that product" ##(product.id) is not used?") if can? (:destroy, product)%>
so when you click on this link a DELETE
request would be sent which would be mapped to your route for products#destroy
, i.e.,
DELETE /products/:id(.:format) products#destroy
Also, you need to update the destroy
action as below:
def destroy
@product = Product.find(params[:id])
@product.destroy
redirect_to products_url ## products_url notice plural products
end
products_url
would redirect you to index
page of products
.
As product_path
is for show
, i.e., to show a particular product. You can't show a deleted product that too without providing an id
to product_path
. So, logically you should be redirecting to index
action with products_url
(NOTE using **_url
for redirection is advisable but you could also use products_path
)