2015-10-29 39 views
1

我已經看到類似的問題發佈了關於這個問題,但提供的解決方案似乎並不是我的問題。Rails無法找到行動'destroy'錯誤

我有一個頁面,用戶可以選擇編輯或刪除位置的文件,它與此每個塊編碼:

<%current_user.locations.reverse.each do |l|%> 
<%=l.address %> 
<a href=<%= edit_location_path(l) %> class="btn btn-primary">Edit</a> 
| <%= link_to "delete" class="bg-danger", l, method: :delete, 
           data: { confirm: "You sure?" } %> 
<br> 
<%end%> 

我在我的路線(有兩種,試圖解決這個錯誤):

get 'locations/:id/delete' => 'locations#destroy' 
get 'locations/:id/destory' => 'locations#destory' 
resources :locations 

而且我這個編碼在locaction控制器:

def destory 
@location = Location.find(params[:id]) 
@deleted_address = @location.address 
@location.destroy 
flash[:danger] = @deleted_address + " deleted" 
redirect_to current_user 
end 

我不能FIGUR e爲什麼rails找不到我的銷燬行爲(重定向適用於其他操作)。

+0

你有'def destory'和'get'locations /:id/destory'=>'locations#destory'' – michaelpri

+0

您應該只需要資源:位置。不知道爲什麼你的路線文件中有兩條獲取線? – DustinFisher

回答

0

這些問題在我跳出:

首先,解決destory錯別字。其次,使用HTTP DELETE動詞destroy

delete 'locations/:id/destroy' => 'locations#destroy' 

最後,link_to應指定位置路徑。

<%= link_to "delete", location_path(l), class: "bg-danger", method: :delete, 
     data: { confirm: "You sure?" } %> 
1

您正在呼叫method: :delete在您的鏈接,這是正確的。

我看到的唯一的其他問題是你拼寫錯誤destroy。你拼寫爲destory,比如「dee-stor-ee」。

我會刪除這條路線,以及:

get 'locations/:id/destory' => 'locations#destory' #=> wouldn't work anyways because it's not a "delete" request 

,因爲你已經調用resources :locations

1

這樣做:

#config/routes.rb 
resources :locations #-> DELETE url.com/locations/:id goes to destroy action 

#view 
<%current_user.locations.reverse.each do |l|%> 
    <%=l.address %> 
    <%= link_to "Edit", l, class: "btn btn-primary" %> 
    <%= link_to "Delete", l, method: :delete, class: "bg-danger", data: { confirm: "You sure?" } %> 
<% end %> 

這將請求發送到您的locations#destroy行動。

你目前的問題是,你打電話link_to有一些奇怪的排序:

<%= link_to "delete" class="bg-danger", l, method: :delete, data: { confirm: "You sure?" } %> 

...應該是...

<%= link_to "Delete", l, method: :delete, class: "bg-danger", data: { confirm: "You sure?" } %> 

由於每docs

link_to(name = nil(link text), options = nil (controller/url), html_options = nil(class/id/data), &block)