Issue in rails 4.0 with creating a link_to for a delete action
If you use (which is adviced) resources
:a) Your action for deleting records should be named destroy
.b) Game is searched for with :id
parameter:
def destroy @game = Game.find(params[:id]) @game.destroy redirect_to :action => 'index'end
c) Your link should be:
<%= link_to 'Delete', t, method: :delete %>
since the path is the same as for the show
action, the only thig that changes is HTTP method.
The format for the delete call is:
<%= link_to 'Delete', game_path(t.id), :method => :delete %>
use rake routes
to learn about the available routes, including generated route helpers, and the controller/action handling the request.
I had similar issue on rails 4.2.1, even with the :method => :delete on link_to it still routes to show method.
But using button_to method as below works!
<%= button_to "delete", article_path(:id => article.id), :method => :delete %>
button_to creates a form around the button and then posts to the delete method, by adding a hidden field named _method
with value delete
rails uses this to route to the destroy method in your controller.