2017-07-26 76 views
1

我想只允許刪除,如果有多個用戶與角色「所有者」。我想顯示一條錯誤消息,告知他們無法刪除該用戶(包含角色所有者),如果它是唯一所有者。這是表格的樣子。驗證在ruby中刪除之前的對象集合

enter image description here

def destroy 
    @user = @organization.users 
    .active 
    .find(params[:id]) 

    lastOwner = @organization.users 
    .active 
    .where(role: "owner") 
    .where.not(id: @user.id) 
    .count 
    .zero? 

    if lastOwner 
    flash[:error] = "There must always be at least one owner, please give another user the role first" 
    redirect_to teams_path 
    return 
    else 
    if @user && @user.archive! 

     respond_to do |format| 
     format.html { redirect_to teams_path } 
     format.json { head :no_content } 
     format.js { render :layout => false } 
     end 
    end 
    end 
end 

這是我破壞方法。

正在發生的事情是,我嘗試重定向,但我收到錯誤

Started DELETE "/teams" for ::1 at 2017-07-26 11:45:28 -0400 

ActionController::RoutingError (No route matches [DELETE] "/teams"): 
    actionpack (4.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
    actionpack (4.2.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
    railties (4.2.3) lib/rails/rack/logger.rb:38:in `call_app' 
    railties (4.2.3) lib/rails/rack/logger.rb:20:in `block in call' 
    activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `block in tagged' 
    activesupport (4.2.3) lib/active_support/tagged_logging.rb:26:in `tagged' 
    activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `tagged' 
    railties (4.2.3) lib/rails/rack/logger.rb:20:in `call' 
    actionpack (4.2.3) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
    rack (1.6.4) lib/rack/methodoverride.rb:22:in `call' 
    rack (1.6.4) lib/rack/runtime.rb:18:in `call' 
    activesupport (4.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call' 
    rack (1.6.4) lib/rack/lock.rb:17:in `call' 
    actionpack (4.2.3) lib/action_dispatch/middleware/static.rb:116:in `call' 
    rack (1.6.4) lib/rack/sendfile.rb:113:in `call' 
    railties (4.2.3) lib/rails/engine.rb:518:in `call' 
    railties (4.2.3) lib/rails/application.rb:165:in `call' 
    rack (1.6.4) lib/rack/content_length.rb:15:in `call' 
    puma (3.9.1) lib/puma/configuration.rb:224:in `call' 
    puma (3.9.1) lib/puma/server.rb:602:in `handle_request' 
    puma (3.9.1) lib/puma/server.rb:435:in `process_client' 
    puma (3.9.1) lib/puma/server.rb:299:in `block in run' 
    puma (3.9.1) lib/puma/thread_pool.rb:120:in `call' 
    puma (3.9.1) lib/puma/thread_pool.rb:120:in `block in spawn_thread' 


    Rendered /Users/kristianquincosa/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.7ms) 
    Rendered /Users/kristianquincosa/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb (18.7ms) 

一次,我收到的記錄也沒有T刪除錯誤,但沒有閃過這個錯誤消息,但頁面重定向。當我刷新頁面時,當我看到錯誤信息。請任何幫助,並感謝你!

編輯

這是刪除

link_to('', team_path(user), method: :delete, data: {confirm: "Are you sure you want to delete this member?"}, :remote => true, class: "btn btn-danger fa fa-trash delete_user") 

這些代碼是我的路線

teams GET /teams(.:format)           teams#index 
             POST /teams(.:format)           teams#create 
           new_team GET /teams/new(.:format)          teams#new 
           edit_team GET /teams/:id/edit(.:format)        teams#edit 
            team PATCH /teams/:id(.:format)          teams#update 
             PUT /teams/:id(.:format)          teams#update 
             DELETE /teams/:id(.:format)          teams#destroy 
+1

您要刪除的鏈接有誤。你可以編輯你的問題並添加相關的視圖代碼?屏幕截圖沒有提供信息。 – tadman

+2

Ruby還強烈鼓勵使用小寫的變量和方法名稱,所以'lastOwner'應該是'last_owner'。這是因爲case在Ruby中有意義,常量的前綴是大寫字母。 – tadman

+0

你的'config/routes.rb'是怎麼樣的?你如何生成刪除按鈕? – spickermann

回答

0

嘗試改變redirect_to teams_pathredirect_to teams_urlrespond_to代碼塊。

相關問題