0

Does exist a way to do something like this:

Model.human_attribute_name :attr

but with errors?

Something like this...

Model.human_error_name :attr, :error

For example:

Ticket.human_error_name :ticket_type_id, :no_tickets_left

I want to avoid this on my views:

I18n.t("activerecord.errors.models.ticket.attributes.ticket_type_id.no_tickets_left")
Leantraxxx
  • 4,506
  • 3
  • 38
  • 56

1 Answers1

0

Not that I know of, but it is very easy to add such a method to all your models:

module Hemju
  module HumanErrorName
    extend ActiveSupport::Concern

    def human_error_name(attr, error)
      I18n.t("activerecord.errors.models.#{self.class.to_s}.attributes.#{attr}.#{error}")
    end
  end
end
ActiveRecord::Base.send(:include, Hemju::HumanErrorName)

Activating the extension in initializer:

require 'hemju/human_error_name'

Now every model has a 'human_error_name' method. Remember, every time you change the method, you have to restart the server.

hjuskewycz
  • 1,437
  • 1
  • 14
  • 20