2016-03-05 17 views
0

我正在使用link_to嘗試刪除記錄,但我收到一條錯誤,指出刪除路徑不存在。當查看html表單中的href時,href的形式是:id/controller而不是controller /:id。我有兩個這樣的例子,一個用於Edit,似乎按預期工作,另一個用於Remove,路由似乎關閉。我看到的主要區別是locale沒有在destroy href中定義。任何想法是什麼導致這個問題?我之前以不同的形式看到了這一點,但我開始更頻繁地看到這一點。任何幫助將非常感激。HTML創建href的軌道路線錯誤順序

路線:

  topdressings GET  (/:locale)/topdressings(.:format)        topdressings#index {:locale=>/en|es/} 
         POST (/:locale)/topdressings(.:format)        topdressings#create {:locale=>/en|es/} 
     new_topdressing GET  (/:locale)/topdressings/new(.:format)       topdressings#new {:locale=>/en|es/} 
    edit_topdressing GET  (/:locale)/topdressings/:id/edit(.:format)     topdressings#edit {:locale=>/en|es/} 
         GET  (/:locale)/topdressings/:id(.:format)       topdressings#show {:locale=>/en|es/} 
         PATCH (/:locale)/topdressings/:id(.:format)       topdressings#update {:locale=>/en|es/} 
         PUT  (/:locale)/topdressings/:id(.:format)       topdressings#update {:locale=>/en|es/} 
         DELETE (/:locale)/topdressings/:id(.:format)       topdressings#destroy {:locale=>/en|es/} 

HTML:

<td><%= link_to t(:edit), edit_topdressing_path(topdressing), data: { toggle: "modal", target: "#EditModal_#{topdressing.id}", remote: edit_topdressing_path(topdressing) + "#modal-edit-form" }, remote: true %> 
| 
<%= link_to t(:remove), topdressings_path(topdressing), method: :delete, data: { confirm: t(:confirm_remove_topdressing, device: "#{topdressing.topdressing_device.name}", date: "#{topdressing.date}") } %></td> 

HREF詳細信息頁來源

<td><a data-toggle="modal" data-target="#EditModal_1" data-remote="/en/topdressings/1/edit#modal-edit-form" data-remote="true" href="/en/topdressings/1/edit">Edit</a> 
| 
<a data-confirm="Are you sure you want to delete the record for Device 2 on 2016-02-26" rel="nofollow" data-method="delete" href="/1/topdressings"><span>Remove</span></a></td> 

ERROR ON DELETE:

Started DELETE "/1/topdressing" for 127.0.0.1 at 2016-03-05 14:34:06 -0800 

ActionController::RoutingError (No route matches [DELETE] "/1/topdressing"): 
    web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:22:in `middleware_call' 

HTML形式的詳情:

回答

0

你的第二個鏈接使用topdressing_path網址助手,傳遞給它一個參數:

link_to t(:remove), topdressings_path(topdressing), method: :delete 

但你rake routes顯示,路徑只有在它一個佔位符,該區域設置:

 topdressings GET  (/:locale)/topdressings(.:format) 

您需要改用另一個URL助手。在Rails' resourceful routes你有一個topdressing_path,您可以使用:

link_to t(:remove), topdressing_path(topdressing), method: :delete 

,但我想這不是你如何得到你的路由設置。機會是,你在config/routes.rb有這樣的事情。您可以添加as選項give that route a name,讓您在您的看法用它在你的link_to助手:

scope "(:locale)", locale: /en|nl/ do 
    # Other routes omitted 
    get "topdressings/:id", to: "topdressings#show", as: "topdressing" 
end