在Rails中覆盖ActionView :: Base.field_error_proc
现在我将ActionView :: Base.field_error_proc作为
Proc.new do |html_tag, instance| if html_tag =~ /^<label/ or instance.respond_to?(:object_name) %{#{html_tag}}.html_safe else %{#{html_tag}
}.html_safe end
当我使用client_side_validations gems时,我修改了这个来满足我的一些需求。
现在,’fields_with_error’div包围html_tag(即错误字段是特定的)。 我想知道的是,是否可以修改div class =“fields_with_error”的位置,并将其放在包含error_field的div之后。
例如,
那么div class =“fields_with_error”应该被定义为
任何帮助都会很有价值。
因此,当我使用twitter bootstrap时,我通常会使用类似的东西创建初始化程序
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| html = %(#{html_tag}).html_safe # add nokogiri gem to Gemfile form_fields = [ 'textarea', 'input', 'select' ] elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css "label, " + form_fields.join(', ') elements.each do |e| if e.node_name.eql? 'label' html = %(#{e}).html_safe elsif form_fields.include? e.node_name if instance.error_message.kind_of?(Array) html = %(#{html_tag} #{instance.error_message.uniq.join(', ')}).html_safe else html = %(#{html_tag} #{instance.error_message}).html_safe end end end html end
这只是将常规错误调整为表单中的推特错误:>所以一切都很好看;>。 您可以使用相同的方法。
欢呼,如果这有帮助