2016-04-22 78 views
0

目标是移除一个物品;我得到的印象的错误是在我的路线:没有路线匹配[DELETE]“/”;红宝石在轨道上

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 

如前所述,我觉得这是我的路线,尽管我一直在修补他们。有人看到我误解了什么吗?

谢谢。

回答

0

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

+0

你需要知道你所删除的特定项目的项目路径。所以item_path('@ item')我想? – karina

+0

这样做,谢谢! – sergio

+0

我会给@Hamms的答案他是更完整的答案...也许我应该停止尝试从我的手机回答问题... – karina

0

的问题是:destroy定义DELETE route for /:id, not for /。你只能删除东西不是全部。

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

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

已更改为此,它的工作原理: <%= link_to“完成!”, ** item_path(item)**,方法:: delete,class:'glyphicon glyphicon-ok'%> 非常感谢! – sergio