My rails app serves as API for my android app.
I’m wondering when the request encounters any problem, is there a way to detect which error it is on the server side and return with a specific code?
For example, in my users_controller.rb,
if @user.save
respond_with(@user)
else
render json: {errors: @user.errors.full_messages}
end
The output looks like:
{"errors":["Username can't be blank","Password is too short (minimum is 6 characters)","Email has already been taken"]}
How can I check whether it is because of an error of email or an error of password in the controller? Thanks!
Examine the result. You should be able to figure it out.
Alternatively read the documentation: http://api.rubyonrails.org/classes/ActiveModel/Errors.html
Hint: errors is just an OrderedHash object with some additional convenience methods, like full_messages. It has all the information that you are looking for, just not in full_messages.