mardi 29 novembre 2016

How can I allow multiple error rendering messages in rails api response

I am trying to render error messages if any of the condition fails. How can I pass error messages related to failed conditions

But it gives me AbstractController::DoubleRenderError error

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

Aucun commentaire:

Enregistrer un commentaire