2017-03-02 79 views
1

我使用destroy的Ruby-on-軌道 - 破壞 - 錯誤:沒有路由匹配[GET]

在我看來,我有:

#delete_archive_modal.modal.fade 
.modal-header 
    %h3 
    %i.icon-exclamation-sign 
    Attention 
.modal-body 
    %p= "Are you sure you want to delete this portal?" 
.modal-footer 
    %a.btn{"data-dismiss" => "modal", :href => "#"} Cancel 
    = link_to 'Delete', delete_portal_path(portal) 

路線:

resources :portals do 
resources :pill_tabs, only: [:show, :edit] 

resources :page_urls do 
    collection do 
    get :redirects 
    end 
end 

resources :zero_touch_configs do 
    member do 
    get :history 
    end 
end 

member do 
    get :navigation 
    get :history 
    get :sitemap 
    get :url_list 
    post :generate_sitemap 
    post :add_modules 
    post :archive 
    post :delete 
end 

collection do 
    get :index, path: '/' 
    get :new, path: '/new(/:portal_type)' 
    get :accessible_sites 
    get :archive_index 
    get :delete 
end 

並在我的控制器中:

def destroy 
    @portal = Portal.find(params[:id]) 
    @portal.destroy 
    flash[:notice] = 'Portal deleted successfully.' 
    redirect_to action: :archive_index 
end 

routes:

           portals GET  /portals(.:format)                         portals#index 
                 GET  /portals/new(/:portal_type)(.:format)                     portals#new 
          accessible_sites_portals GET  /portals/accessible_sites(.:format)                     portals#accessible_sites 
           archive_index_portals GET  /portals/archive_index(.:format)                      portals#archive_index 
             delete_portals GET  /portals/delete(.:format)                        portals#delete 
         history_portal_stack_wrapper GET  /portals/:portal_id/stack_wrappers/:id/history(.:format)                stack_wrappers#history 
          drafts_portal_stack_wrapper GET  /portals/:portal_id/stack_wrappers/:id/drafts(.:format)                stack_wrappers#drafts 
          purge_portal_stack_wrapper GET  /portals/:portal_id/stack_wrappers/:id/purge(.:format)                stack_wrappers#purge 
        all_drafts_portal_stack_wrappers GET  /portals/:portal_id/stack_wrappers/drafts(.:format)                 stack_wrappers#all_drafts 
           portal_stack_wrappers GET  /portals/:portal_id/stack_wrappers(.:format)                   stack_wrappers#index 
                 POST  /portals/:portal_id/stack_wrappers(.:format)                   stack_wrappers#create 
          new_portal_stack_wrapper GET  /portals/:portal_id/stack_wrappers/new(.:format)                  stack_wrappers#new 
          edit_portal_stack_wrapper GET  /portals/:portal_id/stack_wrappers/:id/edit(.:format)                 stack_wrappers#edit 
           portal_stack_wrapper GET  /portals/:portal_id/stack_wrappers/:id(.:format)                  stack_wrappers#show 
                 PATCH  /portals/:portal_id/stack_wrappers/:id(.:format)                  stack_wrappers#update 
                 PUT  /portals/:portal_id/stack_wrappers/:id(.:format)                  stack_wrappers#update 
                 DELETE /portals/:portal_id/stack_wrappers/:id(.:format)                  stack_wrappers#destroy 
         history_portal_config_bundle GET  /portals/:portal_id/config_bundles/:id/history(.:format)                config_bundles#history 
           portal_config_bundles GET  /portals/:portal_id/config_bundles(.:format)                   config_bundles#index 
                 POST  /portals/:portal_id/config_bundles(.:format)                   config_bundles#create 
          new_portal_config_bundle GET  /portals/:portal_id/config_bundles/new(.:format)                  config_bundles#new 
          edit_portal_config_bundle GET  /portals/:portal_id/config_bundles/:id/edit(.:format)                 config_bundles#edit 
           portal_config_bundle GET  /portals/:portal_id/config_bundles/:id(.:format) 

但我得到一個路由錯誤,不知道從哪裏何去何從......

No route matches [GET] "/portals/asdg/delete"

人人都可以分享指南或指向我的文檔,可以幫助我瞭解這裏有什麼問題?

+0

能否請您顯示來自'這裏耙routes' delete_portal_path? – ashvin

+0

@ashvin添加了路由。謝謝 – essentialgreen

+0

不客氣:) – ashvin

回答

4

嘗試

= link_to 'Delete', portal_path(portal), method: :delete 

這裏是所有7條路線portal

   portals GET /portals(.:format)         portals#index 
         POST /portals(.:format)         portals#create 
      new_portal GET /portals/new(.:format)        portals#new 
      edit_portal GET /portals/:id/edit(.:format)      portals#edit 
       portal GET /portals/:id(.:format)        portals#show 
         PATCH /portals/:id(.:format)        portals#update 
         PUT /portals/:id(.:format)        portals#update 
         DELETE /portals/:id(.:format)        portals#destroy 

而在你的路線,你有delete_portal路徑會存在,因爲

member do 
    post :delete 
end 

如果你想調用這個,然後你在con中定義了這個動作的方法troller並調用此路徑method: :post

但是在RESTful路由中刪除動作總是有DELETE方法,參見上面的路由。

+0

謝謝@ashvin!我如何在控制器中定義方法? 'def delete'? – essentialgreen

+0

是定義控制器中的新方法'def delete#爲delete_portal_path設置一些東西end' – ashvin

+0

但是'destroy'動作已經在你的控制器中使用該方法使用'portal_path(portal),method :: delete'刪除對象,而不是'delete'的自定義路由。 – ashvin

1

您需要定義HTTP方法,當您使用刪除

= link_to 'Delete', portal_path(portal), method: :delete 

它將工作。

3

您的link_to標籤有兩個問題。

  1. 您使用delete_portal_path(portal)但如果你的控制檯上運行rake routes你會看到沒有這樣的路徑存在。它有portal_path(portal)

  2. 在Show(GET默認),update(PUT)和delete(DELETE)的情況下,您需要在路由中指定方法類型。因爲全部共享相同的路徑portal_path(portal)

因此,最終的路線應該是:

= link_to 'Delete', portal_path(portal), method: :delete 

更多細節here

相關問題