【问题标题】:AbstractController::DoubleRenderError that shouldn't be不应该出现的 AbstractController::DoubleRenderError
【发布时间】:2011-03-24 06:22:07
【问题描述】:

当我在我的用户控制器中点击这个销毁方法时,我遇到了以下错误。

AbstractController::DoubleRenderError(在此操作中多次调用渲染和/或重定向。 请注意,您只能 调用渲染或重定向,每个操作最多一次。另请注意 既不重定向也不渲染终止动作的执行,所以如果 你想在重定向后退出一个动作,你需要做一些事情 比如“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

有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我已将“并返回”添加到控制器,但我仍然收到同样的错误。
    • 我不明白为什么,但如果我将其更改为 older respond_to 块之一,错误就会消失。
    【解决方案2】:

    head(:ok) 不会返回您可以respond_with 的内容。 head :ok 呈现没有正文的 200。 respond_with 通过响应器呈现您传递给它的对象的一些表示。 head 调用renderrespond_with 调用render,因此出现双重渲染错误。

    您应该将该行更改为 head :ok

    【讨论】:

      猜你喜欢
      • 2015-11-27
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多