ruby-on-rails - AbstractController::DoubleRenderError 不应该的

标签 ruby-on-rails ruby-on-rails-3 internal-server-error respond-to

当我在用户 Controller 中调用此销毁方法时,出现以下错误。

AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".):

这很奇怪,因为老实说我只回复一次电话。

这是我的行动:

def destroy
  user = User.find(params[:id])
  if user.has_role_for? current_client

    # then we remove the role
    user.has_no_roles_for! current_client

    # was that the users only role?
    if user.roles.count == 0
      user.destroy
    end

    respond_with head :ok
  else
    respond_with({:error=>'unauthorised'}, :status => :forbidden)
  end
end

有什么想法吗?

最佳答案

尝试在respond_with行后添加“并返回”:

respond_with head :ok and return 

respond_with({:error=>'unauthorised'}, :status => :forbidden) and return

关于ruby-on-rails - AbstractController::DoubleRenderError 不应该的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5415487/

相关文章:

ruby-on-rails - 更改 faker gem 电话号码格式

azure - azure 项目的内部服务器错误

javascript - 在 $.ajax 中使用 "POST"方法传递数据时出现 500 Internal Server Error

ruby-on-rails - 没有这样的文件要加载——rest_client (Rails 3)

ruby-on-rails - 在 Ruby on Rails 中,authenticate_with_http_basic 是做什么的?

ruby-on-rails - Rails:使用参数重定向

Azure Blob 存储 StartCopy,源指向 Cloud Convert

ruby-on-rails - rails : Serializing deeply nested associations with active_model_serializers

ruby-on-rails - Rails 在分配 id 时更新 bool 属性

ruby-on-rails - 如何创建复杂的 Nested -> Belongs To 对象?