2017-07-26 82 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代码块。

相关问题