0

出现一条确认消息,询问您是否确定要删除,但随后播放器停留在那里。

这是我在控制器中的销毁方法

  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
4

5 回答 5

2

您应该指定method: 'delete'默认链接是method: 'get'.

rails router 会看到delete并采取相应的行动

于 2013-03-25T07:20:24.163 回答
0

将删除按钮的 link_to 格式化如下。

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

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

于 2013-03-25T07:21:10.210 回答
0

2个问题:

  • destroy您在控制器中拼写错误。
  • 您需要添加method: :deletelink_to
于 2013-03-25T07:22:36.183 回答
0

你的播放器叫做detroy。你没有收到语法错误吗?你不是处于开发模式吗?

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

纠正你的方法。

于 2013-03-25T07:17:43.387 回答
0

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

这是一个 HTTP DELETE 方法而不是 ruby​​ 方法。

<%= 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?" %>
于 2013-03-25T08:21:10.703 回答