5

私は次のモデルを持っています:

class Evaluation < ActiveRecord::Base
    attr_accessible :product_id, :description, :evaluation_institutions_attributes

    has_many :evaluation_institutions, :dependent => :destroy  
    accepts_nested_attributes_for :evaluation_institutions, :reject_if => lambda { |a| a[:token].blank? }, :allow_destroy => true       

    validate :requires_at_least_one_institution

    private

      def requires_at_least_one_institution
        if evaluation_institution_ids.nil? || evaluation_institution_ids.length == 0
          errors.add_to_base("Please select at least one institution")
        end
      end    
end

class EvaluationInstitution < ActiveRecord::Base

  attr_accessible :evaluation_institution_departments_attributes, :institution_id

  belongs_to :evaluation

  has_many :evaluation_institution_departments, :dependent => :destroy  
  accepts_nested_attributes_for :evaluation_institution_departments, :reject_if => lambda { |a| a[:department_id].blank? }, :allow_destroy => true

  validate :requires_at_least_one_department

  private

    def requires_at_least_one_department
       if evaluation_institution_departments.nil? || evaluation_institution_departments.length == 0
         errors.add_to_base("Please select at least one department")
       end
    end

end

class EvaluationInstitutionDepartment < ActiveRecord::Base
  belongs_to :evaluation_institution
  belongs_to :department
end

EvaluationInstitution と EvaluationInstitutionDepartment のネストされた属性を含む評価用のフォームがあるため、フォームは 3 レベルにネストされています。レベル 3 は私に問題を与えています。

エラーは想定どおりにトリガーされますが、requires_at_least_one_department でエラーがトリガーされると、テキストは次のようになります。

評価機関拠点 少なくとも1つの部門を選択してください

メッセージは「少なくとも 1 つの部門を選択してください」と表示されます。

「評価機関ベース」の削除方法を教えてください。

4

4 に答える 4

6

Rails 3.2 では、メソッド full_message の実​​装を見ると、I18n を介して "%{attribute} %{message}" の形式でエラー メッセージが表示されることがわかります。

これは、次のように I18n ロケールで表示形式をカスタマイズできることを意味します。

activerecord:
  attributes:
    evaluation_institutions:
      base: ''

それはあなたが望むようにプレフィックス「評価機関ベース」を取り除くでしょう。

于 2012-05-08T21:39:34.070 に答える
1

次のモンキー パッチをイニシャライザに追加すると、3.2.3 で dynamic_form を使用して作業が行われました。

class ActiveModel::Errors
  #exact copy of dynamic_form full_messages except 'attr_name = attr_name.sub(' base', ':')'
  def full_messages        
    full_messages = []

    each do |attribute, messages|
      messages = Array.wrap(messages)
      next if messages.empty?

      if attribute == :base
        messages.each {|m| full_messages << m }
      else
        attr_name = attribute.to_s.gsub('.', '_').humanize
        attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
        attr_name = attr_name.sub(' base', ':')
        options = { :default => "%{attribute} %{message}", :attribute => attr_name }

        messages.each do |m|
          if m =~ /^\^/
            options[:default] = "%{message}"
            full_messages << I18n.t(:"errors.dynamic_format", options.merge(:message => m[1..-1]))
          elsif m.is_a? Proc
            options[:default] = "%{message}"
            full_messages << I18n.t(:"errors.dynamic_format", options.merge(:message => m.call(@base)))
          else
            full_messages << I18n.t(:"errors.format", options.merge(:message => m))
          end            
        end
      end
    end

    full_messages
  end
end

dynamic_form を使用していない場合は、代わりに以下を試してください (フォーム gem が dynamic_form のように errors.full_messages をオーバーライドしない限り):

class ActiveModel::Errors        
    #exact copy of Rails 3.2.3 full_message except 'attr_name = attr_name.sub(' base', ':')'
    def full_message(attribute, message)
      return message if attribute == :base
      attr_name = attribute.to_s.gsub('.', '_').humanize
      attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
      attr_name = attr_name.sub(' base', ':')
      I18n.t(:"errors.format", {
        :default   => "%{attribute} %{message}",
        :attribute => attr_name,
        :message   => message
      })
    end   
end

元のコードへの唯一の変更は、次の行です。

attr_name = attr_name.sub(' base', ':')

提案を歓迎します。

于 2012-04-26T03:14:28.300 に答える