【问题标题】:No route matches [DELETE] "/" ; ruby on rails没有路线匹配 [DELETE] "/" ;铁轨上的红宝石
【发布时间】:2016-04-23 08:34:58
【问题描述】:

目标是移除一个项目;我的印象是我的路线中有错误:

Rails.application.routes.draw do
  devise_for :users
  resources :users, only: [:show, :destroy]
  resources :items, only: [:new, :index, :create, :destroy]
  resources :welcome, only: [:index]
  get 'items/new'    
  root 'items#index'
end

部分如下:

<p><%= item.name %></p>
<%= link_to "Done!", root_path, method: :delete, class: 'glyphicon glyphicon-ok' %>

为了以防万一,以下是我的 items_controller 中的销毁操作:

  def destroy
    @item = Item.find(params[:id])

    if @item.destroy
      flash[:notice] = "You accomplished a task!"
      redirect_to root_path
    else
      flash.now[:alert] = "There was an error deleting the task. Try again."
    end
  end

如前所述,我觉得这是我的路线,尽管我已经对它们进行了一段时间的修改。有人看到我误解了什么吗?

谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby routes partials


    【解决方案1】:

    你不应该使用根路径,你应该使用项目路径

    【讨论】:

    • 您需要要删除的特定项目的项目路径。所以 item_path(@item) 我想?
    • 我会给@Hamms 的答案,他是更完整的答案...也许我应该停止尝试用手机回答问题...
    【解决方案2】:

    问题在于:destroy 定义了一个DELETE route for /:id, not for /。您只能删除 something 而不是所有内容。

    您的link_to 应该链接到特定项目的删除路径,而不是root_path 或项目索引:

    <%= link_to "Done!", item, method: :delete, class: 'glyphicon glyphicon-ok' %>
    

    【讨论】:

    • 把它改成这个并且它工作了:item_path(item), method: :delete, class: 'glyphicon glyphicon-ok' %> 非常感谢!
    猜你喜欢
    • 2011-07-13
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    相关资源
    最近更新 更多