在ruby

时间:2017-07-26 16:02:23

标签: ruby-on-rails ruby web-applications

如果有多个用户具有“所有者”角色,我试图仅允许删除。我想显示一条错误消息,如果它是唯一的所有者,则无法删除用户(具有角色所有者)。这就是表格的样子。

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)

一旦我收到错误,记录不会删除,但错误消息没有闪烁,但页面被重定向。当我刷新页面时,我看到错误消息。请帮助,谢谢!

修改

这是删除的代码

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 个答案:

答案 0 :(得分:0)

尝试在redirect_to teams_path代码块中将redirect_to teams_url更改为respond_to