Ruby-on-rails - 销毁 - 错误:没有路由匹配[GET]

时间:2017-03-02 04:35:38

标签: ruby-on-rails

我正在使用destroy

在我看来,我有:

#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

路线:

                                              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"

任何人都可以分享指南或指向我帮助我了解这里有什么问题的文档吗?

3 个答案:

答案 0 :(得分:4)

尝试

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

以下是portal

的全部7条路线
              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

如果您想调用此方法,那么您已在控制器中为此操作定义方法,并使用method: :post

调用此路径

但是在RESTful路由中,删除操作总是DELETE方法,请参阅上面的路由。

答案 1 :(得分:3)

link_to代码中存在两个问题。

  1. 您使用了delete_portal_path(portal),但如果您在控制台上运行rake routes,则会看到没有此类路由存在。它有portal_path(portal)

  2. 如果显示(GET默认值),更新(PUT)和删除(DELETE),则需要在路径中指定方法类型。因为所有人共享相同的路径portal_path(portal)

  3. 所以你的最终路线应该是:

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

    更多细节here

答案 2 :(得分:1)

使用delete

时需要定义http方法
 = link_to 'Delete', portal_path(portal), method: :delete

它会起作用。