ビューのラベルの後にマークを追加するために、モンゴイド モデルで必要なフィールドを検出しようとしています。これは私が使用している初期化子です。Mongoid の唯一の違いはMongoid::Validations::PresenceValidator
であり、ActiveRecordActiveModel::Validations::PresenceValidator
では であるため、おそらくモンゴイド関連の質問ではないことに注意してください (?):
class ActionView::Helpers::FormBuilder
alias :orig_label :label
# add a 'required' CSS class to the field label if the field is required
def label(method, content_or_options = nil, options = nil, &block)
if content_or_options && content_or_options.class == Hash
options = content_or_options
else
content = content_or_options
end
if object.class.validators_on(method).map(&:class).include? Mongoid::Validations::PresenceValidator
if options.class != Hash
options = {:class => "required"}
else
options[:class] = ((options[:class] || "") + " required").split(" ").uniq.join(" ")
end
end
self.orig_label(method, content, options || {}, &block)
end
end
また、lable.required にアスタリスクを含めるために、このスタイルを使用しています。
/* add required field asterisk */
label.required:after {
content: " *";
}
ラベルに必要なクラスを手動で設定すると、正常に表示されます。問題は、FormBuilder がラベルをまったく変更しておらず、マークが表示されないことです。ファイルはまったく使用されていないようです。イニシャライザとして含めていますが、単純なイベントを書き込むイベントputs "I am here..."
はサーバー コンソールに表示されません。
私は何が欠けていますか?
事前にご回答いただきありがとうございます。