Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealing with active record validation exceptions #23

Closed
cmuhire opened this issue Aug 6, 2015 · 3 comments
Closed

Dealing with active record validation exceptions #23

cmuhire opened this issue Aug 6, 2015 · 3 comments

Comments

@cmuhire
Copy link

cmuhire commented Aug 6, 2015

A failed active record validation (ex: validates :names, presence: true) produces a 422 (Unprocessable Entity) error response which will be handled by Gaffe.

Is there a way to circumvent this so as to deal with failed validations in the original controller without having to resort to a rescue clause in every create/update method?

Thanks for this handy gem by the way!

@remi
Copy link
Member

remi commented Aug 6, 2015

As far as I know, .create, .update and #save don’t raise exceptions — .create!, .update! and #save! do. So if you want to handle errors in the original controller:

class FooController < ApplicationController
  def create
    foo = Foo.create(foo_params)

    if foo.save
      redirect_to foo_path(foo)
    else
      render json: { errors: foo.errors }, status: :unprocessable_entity
    end
  end
end

Gaffe won’t handle any 422 error in the above code.

@cmuhire
Copy link
Author

cmuhire commented Aug 6, 2015

You are right! My bad, I was using a bang update method. Thanks a bunch :)

@remi
Copy link
Member

remi commented Aug 6, 2015

Alright, I’ll close the issue. 😄

@remi remi closed this as completed Aug 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants