2016-04-22 102 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