2013-03-25 86 views
0

确认信息出现,询问您是否确定要删除但玩家在此停留。Rails删除玩家不删除

这是我破坏方法在控制器

def destroy 
    Player.find(params[:id]).destroy 
    redirect_to :action => 'index' 
    end 

这里是我的删除链接

<ul> 
<% @players.each do |p| %> 
<li> 
    <%= link_to p.name, {:action => 'show', :id => p.id} -%> email:<%= p.email %> 
    <%= link_to 'edit', {:action => "edit", 
     :id => p.id} %> 
    <%= link_to "Delete", {:action => 'destroy' ,:id => p.id}, :confirm => "Are you sure you want to delete this item?" %></li> 
<% end %> 
</ul> 
<p><%= link_to "Add new player", {:action => 'new' }%></p> 

这里是日志

Started GET "/players" for 127.0.0.1 at 2013-03-25 15:30:55 +0200 
Processing by PlayersController#index as HTML 
    Player Load (0.1ms) SELECT "players".* FROM "players" 
    Rendered players/index.html.erb within layouts/application (1.0ms) 
Completed 200 OK in 46ms (Views: 45.3ms | ActiveRecord: 0.1ms) 
[2013-03-25 15:30:55] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true 
+0

试试这个<%=的link_to “删除”,{:动作=>' destroy',:method =>'delete',:id => c.id},:confirm =>“你确定要删除这个项目吗?” %> – 2013-03-25 07:17:43

回答

2

您应该指定method: 'delete',因为默认链接为method: 'get'

轨路由器会看到delete和行为accordinly

+0

我应该在我的删除方法? – user1898829 2013-03-25 07:44:11

+0

不,你误解了..做类似'<%= link_to“删除”,{:action =>'destroy',:method =>'delete',:id => c.id}, :confirm => “你确定要删除这个项目吗?” %>' – 2013-03-25 08:15:06

+0

这会让link_to做一个HTTP DELETE请求,这个请求由rails路由器识别并且路由到控制器中的正确动作。如果没有这个,它会执行一个HTTP GET请求,这个请求很可能会转到:show action – 2013-03-25 08:17:01

0

您的播放器称为detroy。 难道你没有语法错误吗?你不是在发展模式吗?

def destroy 
    Player.find(params[:id]).destroy 
    redirect_to :action => 'index' 
end 

更正您的方法。

+0

不,我没有得到一个语法错误,这可能是为什么我没有选择它 – user1898829 2013-03-25 07:40:44

0

其良好格式化的link_to的删除按钮,如下所示。

<%= link_to "Delete", path_for_delete_action(c), :method => :delete, 
    :confirm => "Are you sure you want to delete this item?" %> 

或只是指定控制器方法在当前实现。

0

2个问题:

  • 你在你的控制器拼错destroy
  • 您需要添加method: :deletelink_to
0

指定HTTP方法,你不需要定义删除方法。

这是一个HTTP DELETE方法不是红宝石方法。

<%= link_to "Delete", {:action => 'destroy', :id => c.id, :method => :delete}, 
     :confirm => "Are you sure you want to delete this item?" %> 

我不使用特定的语法,因此,如果不工作,试试这个:

<%= link_to "Delete", player_url(c), :method => :delete, 
     :confirm => "Are you sure you want to delete this item?" %>