0

ActionView::Base.field_error_procRails アプリで検証エラーをカスタマイズするために使用しています。次のように初期化子を作成しました。

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  html = %(<div class="control-group error">#{html_tag}</div>)
  elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css "label, input"
  elements.each do |e|
    if e.node_name.eql? 'label'
      e['class'] = "control-label"
      html = %(#{e})
    elsif e.node_name.eql? 'input'
      html = %(<div class="controls">)
      if instance.error_message.kind_of?(Array)
        html += %(#{html_tag}<span class="help-inline">&nbsp;#{instance.error_message.first}</span>)
      else
        html += %(#{html_tag}<span class="help-inline">&nbsp;#{instance.error_message}</span>)
      end
      html += %(</div>)
    end
  end
  html.html_safe
end

fisheyeサブドメインがそうである場合にのみマークアップをカスタマイズしたいのですが、それ以外の場合は別のマークアップを使用したいと考えています。どうすればそれを達成できますか?instanceブロックに渡されるので可能だと思います。

4

0 に答える 0