I'm trying to make a button work deleting a product from my app, but when i do it it show the error:
No route matches [DELETE] "/"
I search for this problem but i couldn't find something that really helped me.
This is my routes file :
Rails.application.routes.draw do
resources :despensa, only: [:new, :create, :destroy]
root to: "despensa#index"
end
This is my Controller:
class DespensaController < ApplicationController
def index
@produtos = Produto.order :nome
@produto_menor_quantidade = Produto.order(:quantidade).limit 1
end
def create
produto = params.require(:produto).permit(:nome, :quantidade, :unidade_de_medida)
Produto.create produto
redirect_to root_path
end
def destroy
id = params[:id]
Produto.destroy id
redirect_to root_path
end
end
And this is the part of the button:
<% @produtos.each do |produto| %>
<tr>
<td><%= produto.nome%></td>
<td><%= produto.quantidade %></td>
<td><%= produto.unidade_de_medida %></td>
<td><%= button_to "Remover", despensa_path(id), method: :delete ,
class:"btn btn-danger",
:onclick => "return confirm('Tem certeza que deseja remover #{produto.nome} da despensa?')" %>
</td>
</tr>
<% end %>
And finally, this are how my routes looks like:
despensa_index_path POST /despensa(.:format)
despensa#create
new_despensa_path GET /despensa/new(.:format)
despensa#new
despensa_path DELETE /despensa/:id(.:format)
despensa#destroy
root_path GET /
despensa#index
I saw something about jquery, but im not sure if that is the problem, actually i really dont know what is happening here. It was supossed to erase the product when u click the button "remover".