class UsersController < ApplicationController def create # In some sense, we don't really care about the controller at all, except that we # need it for emitting the correct HTTP responses at the end (redirect_to, render, head, etc) # # By telling the SignupUser what to do in each case (failure/success), we can ensure that # the controller outputs the right response, while hiding the logic of success/failure from # the controller itself. # # (see: Tell, Don't Ask) SignupUser .new(user_attributes) .on(:user_signup_succeeded) { |user| render json: { user: user }, status: 201 } .on(:user_signup_failed) { |errors| render json: { errors: errors }, status: 400 } .run end private def user_attributes params.require(:user).permit(:email, :password) end end