AbstractController :: DoubleRenderError不应该

d2k*_*agw 3 ruby-on-rails respond-to internal-server-error ruby-on-rails-3

当我在我的用户控制器中点击这个destroy方法时,我一直收到以下错误.

AbstractController :: DoubleRenderError(在此操作中多次调用渲染和/或重定向.请注意,您可能只调用渲染或重定向,每次操作最多一次.另请注意,重定向和渲染都不会终止执行操作,因此如果要在重定向后退出操作,则需要执行类似"redirect_to(...)并返回"的操作.):

这是一个奇怪的,因为我老实说只响应一次电话.

这是我的行动:

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
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Yul*_*ule 5

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

respond_with head :ok and return 

respond_with({:error=>'unauthorised'}, :status => :forbidden) and return
Run Code Online (Sandbox Code Playgroud)


小智 5

head(:ok)不回报你能做的事情respond_with. head :ok渲染200没有身体. respond_with通过响应者呈现您传递给它的对象的一些表示. head调用render,respond_with调用render,因此双重渲染错误.

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