Rails删除播放器不删除

时间:2013-03-25 07:13:54

标签: ruby-on-rails ruby-on-rails-3

出现确认消息,询问您是否确定要删除但播放器仍然在那里。

这是我在控制器中的destroy方法

  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

5 个答案:

答案 0 :(得分:2)

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

rails路由器将看到delete并采取相应行动

答案 1 :(得分:0)

您的播放器名为detroy。 你不会得到语法错误?你的开发模式不是吗?

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

纠正你的方法。

答案 2 :(得分:0)

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

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

或者只在当前的实施中指定控制器方法

答案 3 :(得分:0)

2个问题:

  • 您在控制器中拼错了destroy
  • 您需要将method: :delete添加到link_to

答案 4 :(得分: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?" %>