Search code examples
ruby-on-railsrubydeviseruby-on-rails-5rails-api

Rails 5 API always return 200 OK, even if a rollback occurs. Why?


I have a very strange problem. I'm working with Rails 5 and Devise to build json based REST api, but for every request, I get always 200 OK even if a Rollback occurs. For example:

curl -i -H "Content-Type: application/json" -d '{ "user": { "email": "[email protected]", "password": "123457890" } }' -X POST http://api.test.com:3000/users/registration

It normally stores the User into database, but for example if I post again the same request, a Rollback is performed on databse because email is an index (and is correct, is what I want...) but the response is again 200 OK.

Now, Devise controllers are overridden because I need to share them among different namespaces and modules, I override them as Devise/Wiki suggest, with the default behaviour of:

def method
    super
end

So, in a normal application, if some validation fails an error is returned to the User in the corresponding View, with the default controller (that are the same of mine, nothing is changed!). Why with "API only" application, each request seems is going always good? What am I forgetting?

Thanks to all.

EDIT: server output.

Started POST "users/registration" for 127.0.0.1
Processing by Users::RegistrationsController#create as */*
Parameters: {"user"=>{"email"=>"[email protected]", "password"=>" [FILTERED]"}, "registration"=>{"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}}}
(0.3ms)  BEGIN
    User Exists (0.8ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
(0.3ms) ROLLBACK 
    Completed 200 OK in 143ms (Views: 0.1ms | ActiveRecord: 1.4ms)

Solution

  • [SOLVED]

    After 4 days of searching, I forgot the respond_to :json in my application_controller.rb.