在尝试使用Destroy时,没有路由匹配[POST]“/ coachings / 4”

时间:2012-01-23 22:37:00

标签: ruby-on-rails ruby destroy

        coachings GET    /coachings(.:format)                        {:action=>"index", :controller=>"coachings"}
                  POST   /coachings(.:format)                        {:action=>"create", :controller=>"coachings"}
     new_coaching GET    /coachings/new(.:format)                    {:action=>"new", :controller=>"coachings"}
    edit_coaching GET    /coachings/:id/edit(.:format)               {:action=>"edit", :controller=>"coachings"}
         coaching GET    /coachings/:id(.:format)                    {:action=>"show", :controller=>"coachings"}
                  PUT    /coachings/:id(.:format)                    {:action=>"update", :controller=>"coachings"}
                  DELETE /coachings/:id(.:format)                    {:action=>"destroy", :controller=>"coachings"}

我的路线是正确的,这是我的视图索引

<%= link_to'摧毁',指导,:确认'你确定吗?',:method => :destroy%>

这是我的控制器

def destroy
    @coaching = Coaching.find(params[:id])
    @coaching.destroy

    respond_to do |format|
      format.html { redirect_to coachings_path }
      format.json { head :ok }
    end
end

为什么我收到此错误的任何想法?我是RoR的新手,这是我自己完成的第一个项目。

2 个答案:

答案 0 :(得分:2)

使用:delete方法

<%= link_to 'Destroy', coaching, :confirm => 'Are you sure?', :method => :delete %>

答案 1 :(得分:1)

在您的link_to中,您使用的destroy方法不是有效的HTTP动词,因此Rails可能默认为POST。您需要使用DELETE代替:

<%= link_to 'Destroy', coaching, confirm: 'Are you sure?', method: :delete %>