Registering a user through Rails API and returning validation errors -



Registering a user through Rails API and returning validation errors -

i'm trying build controller action create new user in application. want effort create user , if succeeds, homecoming user model , if fails show validation errors. far i've got , running:

# app/controllers/v1/users_controller.rb def create @user = user.create(user_params) end private def user_params params.require(:user).permit(:email, :password) if params[:user] end

and i'm using jbuilder in view so:

# app/views/v1/users/create.json.jbuilder if @user.errors json.errors @user.errors else json.user @user end

this looks it'd work me, when create new user doesn't have validation errors output rather user got created:

{"errors":{}}

how come it's printing out empty errors when user has no validation errors?

in ruby, nil , false evaluate false in if statement. in illustration above, @user.errors activemodel::errors class , hence evaluate true. seek changing code above to:

if @user.errors.present? json.errors @user.errors else json.user @user end

ruby-on-rails validation jbuilder

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -