2013-05-17 2 views
2

보기에서 레이블 뒤에 표시를 추가하려면 mongoid 모델에서 필수 필드를 감지하려고합니다. 이것은 내가 사용하고있는 이니셜 라이저입니다. Mongoid의 유일한 다른 것은 ActiveModel::Validations::PresenceValidator 것 액티브에, Mongoid::Validations::PresenceValidator 것을 어쩌면 그것은 mongoid 관련 질문이 아니다, 주 (?) :별표가있는 필수 필드를 자동으로 표시합니다.

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..." 서버 콘솔에 표시되지 않습니다.

무엇이 누락 되었습니까?

미리 답변 해 주셔서 감사합니다.

+0

밤은 그것 ActionView :: 도우미 :: FormHelper? –

답변

0

시도 어쩌면 당신의 레일 버전이 될 수 같은 문제가 있었다

module ActionView::Helpers::FormHelper 
+1

답변 해 주셔서 감사합니다! 나는 처음에 내가 멍청한, 쉬운 일이라고 생각했다! 하지만 나중에 작동하지 않는 것을 보았습니다. FormHelper는 클래스가 아닙니다 (TypeError). 어쨌든, FormBuilder를 사용하여 다음과 같은 많은 예제를 보았습니다. https://gist.github.com/jordanandree/9d6812f0c99a20cacb2c – josal

+0

이것은 작동하지 않습니다. –

관련 문제