Rails - 异常I18n :: InvalidLocale在一个陌生的地方

时间:2015-02-22 12:20:21

标签: ruby-on-rails exception rails-activerecord rails-i18n

有时铁轨是美丽的,但有时它很糟糕。 特别是当你遇到奇怪的异常时

我有3个简单的模型

/app/models/specification_type.rb

class SpecificationType  < ActiveRecord::Base#< AbstractModel

  has_many :scpecification_type_to_category_relations, class_name: "Relations::SpecificationTypeToCategory"
  has_many :categories, through: :scpecification_type_to_category_relations  

end

/app/models/category.rb

class Category < ActiveRecord::Base#AbstractModel

  has_many :groups
  has_many :products

  has_many :scpecification_type_to_category_relations, class_name: "Relations::SpecificationTypeToCategory"
  has_many :specification_types, through: :scpecification_type_to_category_relations  

end

/app/models/relations/specification_type_to_category.rb

class Relations::SpecificationTypeToCategory < ActiveRecord::Base

  self.table_name = "specification_type_to_category_relations"

  belongs_to :scpecification_type
  belongs_to :category

end

迁移20141231115801_create_specification_types.rb

每次在任何地方(视图或控制台),当我尝试调用@ category.specification_types时 我得到例外:

I18n::InvalidLocale: :en is not a valid locale

rails c

Category.first.specification_types
  Category Load (0.3ms)  SELECT  `categories`.* FROM `categories`  ORDER BY `categories`.`id` ASC LIMIT 1
I18n::InvalidLocale: :en is not a valid locale
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/i18n-0.7.0/lib/i18n.rb:284:in `enforce_available_locales!'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/i18n-0.7.0/lib/i18n.rb:151:in `translate'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/core_ext/array/conversions.rb:68:in `to_sentence'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations.rb:60:in `initialize'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/reflection.rb:840:in `new'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/reflection.rb:840:in `check_validity!'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/association.rb:25:in `initialize'
....

我不明白I18n在哪里

我在项目中使用I18n,但不是在这里。 有2个语言环境,但我根本不使用:en locale。

请帮帮我。

1 个答案:

答案 0 :(得分:3)

这个错误非常原始。 a:规格类型中的额外字母“c”。 但要查看我在application.rb中设置的人类可读异常

config.i18n.enforce_available_locales = false