【问题标题】:RoR - en.yml file validation message for nested object/associationRoR - 嵌套对象/关联的 en.yml 文件验证消息
【发布时间】:2012-08-18 20:01:43
【问题描述】:

在 en.yml 中用于关联的验证消息存在问题。

这是我看到的消息:
Name infos prev first name can't be blank

我想改变 2 件事:
1.“名称信息”是关联模型的名称,我不希望它出现
2. “prev first name”应该改为“Previous First Name”


我一直在谷歌搜索、搜索和尝试不同的东西。从我所看到的,下面的“应该”工作,但它不是(我什至重新启动了应用程序)。

**contact_info.rb:** 
class ContactInfo < ActiveRecord::Base
  has_many :name_infos
  accepts_nested_attributes_for :name_infos, :allow_destroy => true
end


**name_info.rb:**
class NameInfo < ActiveRecord::Base
  belongs_to :contact_info

      validates :prev_first_name, :presence => true, 
                   :if => :name_subcat_is_first_name?

      private

      def name_subcat_is_first_name?
        subcat == 'first'
      end

end

**routes.rb:**    
    resources :users do
       resources :contact_infos do
         collection do
           get 'get_full_fields'
           get 'clear_fields'
         end
       resources :name_infos
    end
  end

**en.yml:**    
en:
  activerecord:
    attributes:
      contact_info:
        name_infos:
          prev_first_name: "Previous First Name"

每当我在控制台中测试它时,它都不会改变:

irb(main):050:0> ContactInfo.human_attribute_name("prev_first_name")
=> "Prev first name"
irb(main):051:0> NameInfo.human_attribute_name("prev_first_name")
=> "Prev first name"

我试过了:

**en.yml:**

en:
  activerecord:
    attributes:
      users:
         contact_info:
           name_infos:
             prev_first_name: "Previous First Name"

但这并没有什么不同。

有人能解释一下吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 validation associations yaml


    【解决方案1】:

    我相信格式是这样的(因为prev_first_name是NameInfo模型的一个属性):

    en:
      activerecord:
        attributes:
          name_info:
            prev_first_name: "Previous First Name"
    

    请注意,型号名称 (name_info) 是单数,而不是复数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 2023-01-21
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多