Ruby 重写Rails中的ActionView::Base.field\u error\u proc

Ruby 重写Rails中的ActionView::Base.field\u error\u proc,ruby,ruby-on-rails-3,Ruby,Ruby On Rails 3,现在我有了ActionView::Base.field\u error\u proc作为 Proc.new do |html_tag, instance| if html_tag =~ /^<label/ or instance.respond_to?(:object_name) %{<div class="field_with_errors">#{html_tag}</div>}.html_safe else %{<div class

现在我有了ActionView::Base.field\u error\u proc作为

Proc.new do |html_tag, instance|
  if html_tag =~ /^<label/ or instance.respond_to?(:object_name)
    %{<div class="field_with_errors">#{html_tag}</div>}.html_safe
  else
    %{<div class="field_with_errors">#{html_tag}<br /><label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
  end
Proc.new do | html|u标记,实例|

如果html_tag=~/^那么,当我使用twitter引导时,我通常会用类似的东西创建一个初始值设定项

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  html = %(<div class="field_with_errors">#{html_tag}</div>).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 = %(<div class="control-group error">#{e}</div>).html_safe
    elsif form_fields.include? e.node_name
      if instance.error_message.kind_of?(Array)
        html = %(<div class="control-group error">#{html_tag}<span class="help-inline">&nbsp;#{instance.error_message.uniq.join(', ')}</span></div>).html_safe
      else
        html = %(<div class="control-group error">#{html_tag}<span class="help-inline">&nbsp;#{instance.error_message}</span></div>).html_safe
      end
    end
  end
  html
end
ActionView::Base.field_error_proc=proc.new do | html_标记,实例|
html=%(#{html_tag}).html_安全
#将nokogiri gem添加到Gemfile
表单_字段=[
“文本区域”,
“输入”,
“选择”
]
elements=Nokogiri::HTML::DocumentFragment.parse(HTML_标记).css“label,”+form_字段.join(“,”)
元素。每个元素都有|
如果e.node_name.eql?“标签'
html=%(#{e}).html\u安全
elsif表单_字段。是否包括?e、 节点名称
if instance.error\u message.kind\u of?(数组)
html=%(#{html_tag}{instance.error_message.uniq.join(',')}).html_安全
其他的
html=%(#{html_tag}{instance.error_message}).html_安全
结束
结束
结束
html
结束
这只是将常规错误调整为表单中的twitter错误:>使所有内容看起来都非常好;>。您可以使用相同的方法

如果这有帮助,干杯

<div class="main_div">
  <%= password_field_tag 'secret', 'Your secret here' %>
</div>
<div class="fields_with_error"> <label class="message"> The error message </label> </div>
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  html = %(<div class="field_with_errors">#{html_tag}</div>).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 = %(<div class="control-group error">#{e}</div>).html_safe
    elsif form_fields.include? e.node_name
      if instance.error_message.kind_of?(Array)
        html = %(<div class="control-group error">#{html_tag}<span class="help-inline">&nbsp;#{instance.error_message.uniq.join(', ')}</span></div>).html_safe
      else
        html = %(<div class="control-group error">#{html_tag}<span class="help-inline">&nbsp;#{instance.error_message}</span></div>).html_safe
      end
    end
  end
  html
end