【问题标题】:No route matches [POST] "/coachings/4" when trying to use Destroy尝试使用 Destroy 时没有路线匹配 [POST] "/coachings/4"
【发布时间】:2012-01-23 22:36:59
【问题描述】:
        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"}

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

: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 的新手,这是我自己完成的第一个项目。

【问题讨论】:

    标签: ruby-on-rails ruby destroy


    【解决方案1】:

    使用:delete方法

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

    【讨论】:

    • Delete:获取“你确定吗?”的 GET 操作对话框。
    【解决方案2】:

    在您的link_to 中,您正在使用destroy 的方法,这不是一个有效的HTTP 动词,因此Rails 可能默认为POST。您需要改用DELETE

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      相关资源
      最近更新 更多