2017-07-26 7 views
1

ロール "owner"を持つ複数のユーザーがいる場合にのみ削除を許可しようとしています。唯一の所有者であれば、ユーザーを削除することはできないというエラーメッセージを(ロール所有者とともに)表示したいと考えています。これはテーブルの外観です。私破壊する方法であるルビで削除する前にオブジェクトのコレクションを検証する

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) 

を受けていますということです。ページをリフレッシュすると、エラーメッセージが表示されます。何か助けてくれて、ありがとう!

EDIT

これは、削除

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'でなければなりません。これは、大文字小文字がRubyで意味を持つため、定数の先頭に大文字が付いているためです。 – tadman

+0

あなたの 'config/routes.rb'はどのように見えますか?どのように削除ボタンを生成しますか? – spickermann

答えて

0

があなたのrespond_toコードブロックにredirect_to teams_urlredirect_to teams_pathを変更しようとする私の経路であるためのコードです。

関連する問題