0

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

3 Answers 3

2

You need to add and return to every render in this block

if user.present?
...
end

to exit function. Example:

render json: {errors: "User not associated with this wbp user"}, status: :unprocessable_entity and return
Sign up to request clarification or add additional context in comments.

1 Comment

Except the last one. That is optional.
0

No duplicate status codes and some refactoring, you can do some of validations in filters in this way your methods will look skinny and good

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

2 Comments

@prem status code are not duplicated in this answer
This way is more cleaner.
0
def create
  error_msgs = Array.new
  if @current_wbp_user && params[:user_id].present? && params[:note_id].present?
    user = User.find_by(id: params[:user_id])
    if user.present?
      if user.access_code != @current_wbp_user.access_code
        error_msgs << "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
          error_msgs << "Invalid note for this user"
        end
      else
        error_msgs << "Note not found"
      end
    else
      error_msgs << "User not found"
    end

    if params[:comment].present? && params[:hashtag].present?
      @comment = @current_wbp_user.wbp_user_comments.build(wbp_user_comments_params)
      if @comment.save
        render json: {success: true}, status: :ok
        return
      else
        error_msgs << "Comment could not be created"
      end
    end
  else
    error_msgs << "Insufficient Information"
  end

  if error_msgs.present?
    render json: {errors: error_msgs}, status: :unprocessable_entity
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.