2014-10-20 52 views
0

銷燬行爲在我的應用程序的一部分中爲我工作,但我無法在另一個使用單獨控制器的視圖中使其工作。沒有路由匹配銷燬行動

我收到錯誤:No route matches {:action=>"destroy", :controller=>"letsgo"}

查看:

<% for letsgo in @letsgos %> 
<li> 
<b>Let's Go...<span class="content"><%= letsgo.content %></span></b> 
<%= link_to 'Delete', { :controller => 'letsgo', :action => 'destroy'}, 
         { :confirm => 'Are you sure?', :method => :delete, :remote => true} %> 
<% end %> 

路線:

resources :letsgos, only: [:create, :destroy] 

LetsGos控制器:

def destroy 
    @letsgo.destroy 
    redirect_to root_url 
    end 

此代碼的工作,如果我下在letsgos觀點:<%= link_to "delete", letsgo, method: :delete, data: { confirm: "You sure?" }%>

銷燬行動工作,如果我的letsgos視圖下工作,但我在不同的文件夾就不再起作用工作。我正在做的是從letsgos表中列出所有content,併爲每個內容提供銷燬操作。

路線:

   letsgos_eatdrink GET  /letsgos/eatdrink(.:format)     letsgos#eatdrink 
      letsgos_listenwatch GET  /letsgos/listenwatch(.:format)    letsgos#listenwatch 
        letsgos_play GET  /letsgos/play(.:format)      letsgos#play 
        letsgos_other GET  /letsgos/other(.:format)      letsgos#other 
       letsgos_explore GET  /letsgos/explore(.:format)     letsgos#explore 
        repost_letsgo POST  /letsgos/:id/repost(.:format)     letsgos#repost 
       interested_letsgo POST  /letsgos/:id/interested(.:format)    letsgos#interested 
           GET  /letsgos(.:format)       letsgos#index 
           POST  /letsgos(.:format)       letsgos#create 
         new_letsgo GET  /letsgos/new(.:format)      letsgos#new 
        edit_letsgo GET  /letsgos/:id/edit(.:format)     letsgos#edit 
           GET  /letsgos/:id(.:format)      letsgos#show 
           PATCH /letsgos/:id(.:format)      letsgos#update 
           PUT  /letsgos/:id(.:format)      letsgos#update 
           DELETE /letsgos/:id(.:format)      letsgos#destroy 
+0

命令的結果是什麼:'''rake routes'''? – 2014-10-20 21:18:48

+0

@TomHert添加路線 – pwz2000 2014-10-20 21:20:41

+0

您是否需要在您的'link_to'刪除行中傳入要刪除的'@ letsgo'對象的':id'? – Alireza 2014-10-20 21:25:06

回答

1

你是不是經過letsgo的ID路線:

<%= link_to 'Delete', { :controller => 'letsgos', :action => 'destroy', :id => letsgo.id }, 
         { :confirm => 'Are you sure?', :method => :delete, :remote => true} %> 

寫在你的路:

letsgo DELETE /letsgos/:id(.:format)      letsgos#destroy 

這不是測試,但應該是這樣的

+0

我在發佈之前實現了'letsgo.id',它給了我一個路由錯誤,所以我刪除了它。爲了給它添加確切的錯誤,沒有路由匹配{:action =>「destroy」,:controller =>「letsgo」,:id => 9}'。 – pwz2000 2014-10-20 21:40:54

+1

所以''':controller =>'letsgos'''也有錯誤,請參閱http://guides.rubyonrails.org/routing.html#static-segments – 2014-10-20 21:43:47

0

這也適用於我!只需在{ :confirm..... }之前添加DATA: ...並確認刪除對話框起作用。

<%= link_to 'Delete', { :controller => 'letsgos', :action => 'destroy', :id => letsgo.id }, 
       data: { :confirm => 'Are you sure?', :method => :delete, :remote => true} %>