8

This is my first project in rails, which is to create a table that will store data about games. I'm able to display data from the table about winner score, loser score, etc. However, I have issues with my table column that contains delete links for each game.

Here's my code in the games controller for the delete method:

def delete
  @game = Game.find(params[:game])
  @game.destroy()
  redirect_to :action => 'index'
end

A snippet of my table code, which includes the line for the link_to command

    <% @games_items.each do |t| %>
     <tr>
        <td><%= t.winner.name %></td>
        <td><%= t.loser.name %></td>
        <td><%= t.challenger.name %></td>
        <td><%= t.winner_score %></td>
        <td><%= t.loser_score %></td>
        <td><%= link_to 'Delete', delete_game_path(id: t.id)%></td>
     </tr>
    <% end %>

In the routes file I called

resources :games

Which, to my knowledge, helps generate the base routing. Could anyone help me figure out why my link_to is not working?

Nimish
  • 1,053
  • 13
  • 29
Michael Liu
  • 1,071
  • 3
  • 12
  • 18

4 Answers4

17

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.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
  • Where should I look to see what my actions should be named? Is that contained in the rake routes method? – Michael Liu Jul 10 '13 at 15:28
  • @MichaelLiu yes, the output of `rake routes` task contains appropriate action names. – Marek Lipka Jul 10 '13 at 15:29
  • @MarekLipka I'm having the exact same problem but I can't get the `:delete` method to work. No matter what I do, it results in a `get` request. Any ideas why? ` <%= link_to("Delete!", {:controller=>'projects', :action=>'destroy', :id=>project.hashed_id}, {method: :delete} ) %> ` – emersonthis Aug 17 '13 at 01:12
  • @Emerson your JavaScript doesn't probably work. Check your browser's console for possible JS errors. – Marek Lipka Aug 17 '13 at 09:34
6

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.

boulder
  • 3,256
  • 15
  • 21
5

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.

Kingsley Ijomah
  • 3,273
  • 33
  • 25
  • I had this same problem where `link_to` wasn't working with `method: :delete`, but solved it by adding `jquery_ujs` to my application.js https://stackoverflow.com/a/23331608/4249632 – Ryan Hertz Jul 12 '17 at 15:42
0

Try to use <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> before <%= javascript_include_tag "application" %> in your layout, and also delete

//= require jquery

line in your application.js. This was the case for me. No idea why it didn't worked with original rails jquery.js file.

Gediminas Šukys
  • 7,101
  • 7
  • 46
  • 59