在Rails 4.0中,为删除操作创建link_to时出现问题 [英] Issue in rails 4.0 with creating a link_to for a delete action

查看:85
本文介绍了在Rails 4.0中,为删除操作创建link_to时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Rails中的第一个项目,它是创建一个表来存储有关游戏的数据。我可以显示表格中有关获胜者分数,失败者分数等的数据。但是,我的表格列存在问题,其中包含每个游戏的删除链接。

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

我的表代码的一个片段,其中包括link_to命令

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 %>

在路由文件中,我叫

resources :games

据我所知,这有助于产生基本路由。有人可以帮我弄清楚为什么我的link_to无法正常工作吗?

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

推荐答案

如果使用(建议)资源
a)删除记录的操作应命名为 destroy
b)使用:id 参数搜索游戏:

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)您的链接应为:

<%= link_to 'Delete', t, method: :delete %>

因为路径与显示操作,唯一更改的选项是HTTP方法。

since the path is the same as for the show action, the only thig that changes is HTTP method.

这篇关于在Rails 4.0中,为删除操作创建link_to时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆