Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

undefined method 'i18n_scope' raised by ActiveModel::Errors when validated model is not extended with ActiveModel::Translation. Incorrect documentation about ActiveModel::Errors class.

#5198

This is extract from ActiveModel::Errors documentation:

  # A minimal implementation could be:
  # 
  #   class Person
  #   
  #     # Required dependency for ActiveModel::Errors
  #     extend ActiveModel::Naming
  # 
  #     def initialize
  #       @errors = ActiveModel::Errors.new(self)
  #     end
  #   
  #     attr_accessor :name
  #     attr_reader   :errors
  #   
  #     def validate!
  #       errors.add(:name, "can not be nil") if name == nil
  #     end
  #   
  #     # The following methods are needed to be minimally implemented
  #
  #     def read_attribute_for_validation(attr)
  #       send(attr)
  #     end
  #   
  #     def ErrorsPerson.human_attribute_name(attr, options = {})
  #       attr
  #     end
  #   
  #     def ErrorsPerson.lookup_ancestors
  #       [self]
  #     end
  #   
  #   end
  #
  # The last three methods are required in your object for Errors to be
  # able to generate error messages correctly and also handle multiple
  # languages.  Of course, if you extend your object with ActiveModel::Translations
  # you will not need to implement the last two.  Likewise, using
  # ActiveModel::Validations will handle the validation related methods
  # for you.

Problem 1: I think it should state that Person.human_attribute_name and Perons.lookup_ancestors are necessary. They should be defined for Person class, not for ErrorsPerson class.

Problem 2: Implementing those methods on Person class is not enough. When you do so it leads to the exception:

undefined method `i18n_scope' for Person:Class

activemodel (3.0.0.beta4) lib/active_model/errors.rb:261:in `block in generate_message'
activemodel (3.0.0.beta4) lib/active_model/errors.rb:260:in `map'
activemodel (3.0.0.beta4) lib/active_model/errors.rb:260:in `generate_message'
activemodel (3.0.0.beta4) lib/active_model/errors.rb:182:in `add'

Problem 3: To walk around second problem the class should be extended with ActiveModel::Translation, not ActiveModel::Translations as the documentation states.

Reported by Robert Pankowecki · July 26th, 2010 @ 03:33 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    November 7th, 2010 @ 04:54 PM

  2. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 9th, 2011 @ 12:32 AM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 9th, 2011 @ 12:32 AM