Is there a way to return error code in addition to error message in rails active record validation?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 09:27:38

问题


In rails activerecord validation, normally if a validation fails, it will add an error message in the errors attribute of models, however our clients demands an error code be returned in addition to error message, for example, we have a Bill model, which has a msisdn attribute, if msisdn is null, the error code is 101, if the msisdn doesn't complaint with MSISDN format, the error code is 102, when the client submits a request through REST interface, and if the validation fails, we should return a json object like

bill: {
    error_code: 101,
    error_message: "msisdn can't be null"
}

Is there a way to tell activerecord to generate an error code in addition to error messages? Thanks a lot.


回答1:


errors is just a plain hash, with the key represents the attribute which has an error, and the value represents the error message. So technically your requirement is doable by replacing the text message with a hash. But the downside is you may need to do more things to show the errors in new format.

For example, use a custom validator to add error code

class Foo < ActiveRecord::Base
  attr_accessible :msiisnd
  validate :msiisdn_can_not_be_blank

  def msiisdn_can_not_be_blank
    if msiisdn.blank?
      errors.add(:msiisdn, {code: 101, message: "cannot be blank"})
    end
  end
end

Then use it

foo = Foo.new
foo.errors.count
#=> 0
foo.valid?
#=> false
foo.errors.count
#=> 1
foo.errors[:msiisdn]
#=> [{ code: 101, message: "can not be blank"}]
foo.errors[:msiisdn][0][:code]
#=> 101

So you can use it. But you need to do more work when you need to show the errors correctly, say displaying errors in a form, as this is not a convention.




回答2:


Rails 5 will include a similar feature with foo.errors.details. You can easily use this to build better api errors.

class Person < ActiveRecord::Base
 validates :name, presence: true
end

person = Person.new
person.valid?
person.errors.details
# => {name: [{error: :blank}]}

If you prefer code errors as numbers, you can easily match numbers with the error keys.

For Rails 3 & 4 you can use this gem https://github.com/cowbell/active_model-errors_details witch is the exact same feature backported.




回答3:


Rails 5+ or Rails 4 and active_model-errors_details gem:

class User
  include ActiveModel::Model
  validate :forbid_special_email
  attr_accessor :email

  private

  def forbid_special_email
    errors.add(:email, 'is forbidden', code: 1234) if email == 'john@example.com'
  end
end

user = User.new(email: 'john@example.com')
user.validate
user.errors.full_messages # ["Email is forbidden"]
user.errors.details # {:email=>[{:error=>"is forbidden", :code=>1234}]}


来源:https://stackoverflow.com/questions/19672784/is-there-a-way-to-return-error-code-in-addition-to-error-message-in-rails-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!