Ruby on rails 如何允许在rails api响应中呈现多个错误消息

Ruby on rails 如何允许在rails api响应中呈现多个错误消息,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,如果任何条件失败,我将尝试呈现错误消息。如何传递与失败条件相关的错误消息 但它给了我AbstractController::DoubleRenderError错误 def create if @current_wbp_user && params[:user_id] && params[:note_id] && params[:comment] && params[:hashtag] user = User.fi

如果任何条件失败,我将尝试呈现错误消息。如何传递与失败条件相关的错误消息

但它给了我
AbstractController::DoubleRenderError
错误

def create
    if @current_wbp_user && params[:user_id] && params[:note_id] && params[:comment] && params[:hashtag]
      user = User.find_by(id: params[:user_id])
      if user.present?
        if user.access_code != @current_wbp_user.access_code
          render json: {errors: "User not associated with this wbp user"}, status: :unprocessable_entity
        end
        note = Note.find_by(id: params[:note_id])
        if note.present?
          if note.user_id != user.id
            render json: {errors: "Invalid note for this user"}, status: :unprocessable_entity
          end
        else
          render json: {errors: "Note not found"}, status: :unprocessable_entity
        end
      else
        render json: {errors: "User not found"}, status: :unprocessable_entity
      end
      @comment = @current_wbp_user.wbp_user_comments.build(wbp_user_comments_params)
      if @comment.save
        render json: {success: true}, status: :ok
      else
        render json: {errors: "Comment could not be created"}, status: :unprocessable_entity
      end
    else
      render json: {errors: "Insufficient Information"}, status: :unprocessable_entity
    end
  end

您需要添加
并将
返回到此块中的每个渲染

if user.present?
...
end
退出函数。例如:

render json: {errors: "User not associated with this wbp user"}, status: :unprocessable_entity and return

您需要添加
并将
返回到此块中的每个渲染

if user.present?
...
end
退出函数。例如:

render json: {errors: "User not associated with this wbp user"}, status: :unprocessable_entity and return

没有重复的状态代码和一些重构,您可以在过滤器中进行一些验证,这样您的方法看起来既简洁又好

def create
    sucess = false
    if @current_wbp_user && [:user_id, :note_id, :comment, :hashtag].all? {|s| params.key? s}
      user = User.find_by(id: params[:user_id])
      if user.present?
        if user.access_code != @current_wbp_user.access_code
          message = "User not associated with this wbp user"
        end
        note = Note.find_by(id: params[:note_id])
        if note.present?
          if note.user_id != user.id
            message = "Invalid note for this user"
          end
        else
          message = "Note not found"
        end
      else
        message = "User not found"
      end
      @comment = @current_wbp_user.wbp_user_comments.build(wbp_user_comments_params)
      if @comment.save
        sucess = true
      else
        message = "Comment could not be created"
      end
    else
      message = "Insufficient Information"
    end

    if sucess
      render json: {success: true}, status: :ok
    else
      render json: {errors: message}, status: :unprocessable_entity
    end
  end

没有重复的状态代码和一些重构,您可以在过滤器中进行一些验证,这样您的方法看起来既简洁又好

def create
    sucess = false
    if @current_wbp_user && [:user_id, :note_id, :comment, :hashtag].all? {|s| params.key? s}
      user = User.find_by(id: params[:user_id])
      if user.present?
        if user.access_code != @current_wbp_user.access_code
          message = "User not associated with this wbp user"
        end
        note = Note.find_by(id: params[:note_id])
        if note.present?
          if note.user_id != user.id
            message = "Invalid note for this user"
          end
        else
          message = "Note not found"
        end
      else
        message = "User not found"
      end
      @comment = @current_wbp_user.wbp_user_comments.build(wbp_user_comments_params)
      if @comment.save
        sucess = true
      else
        message = "Comment could not be created"
      end
    else
      message = "Insufficient Information"
    end

    if sucess
      render json: {success: true}, status: :ok
    else
      render json: {errors: message}, status: :unprocessable_entity
    end
  end
def创建
错误\u msgs=Array.new
如果@current_wbp_user&¶ms[:user_id].present?&¶ms[:note_id]。是否存在?
user=user.find_by(id:params[:user_id])
如果user.present?
如果user.access_代码!=@当前用户访问代码
错误\u msgs
def create
错误\u msgs=Array.new
如果@current_wbp_user&¶ms[:user_id].present?&¶ms[:note_id]。是否存在?
user=user.find_by(id:params[:user_id])
如果user.present?
如果user.access_代码!=@当前用户访问代码

错误\u msgs,最后一个除外。这是可选的。除了最后一个。这是可选的。@prem状态代码在此应答中不重复此方式更干净。@prem状态代码在此应答中不重复此方式更干净。