2017-03-02 83 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

相关问题