mardi 4 août 2015

Refactoring multiple render in controller

In my rails controller, I have to check after getting @group with before_action that this group is not system.

But I have lot's of repetition in my controller. I've tried to turn into a separate method but I get the classic :

Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".

Here is a part of my code

def destroy
  if @group.is_system?
    render json: { errors: 'You can\'t delete a group system' }, status: 403
    return
  end

  ...
end

def update
  if params[:group] && !params[:group].empty?
    if @group.is_system?
      render json: { errors: 'You can\'t edit a group system' }, status: 403
      return
    end

    ...

  else
    render json: { errors: 'Missing correct parameters' }, status: :unprocessable_entity
  end
end

.....

Aucun commentaire:

Enregistrer un commentaire