How to change the default rails error div “field_with_errors”

前端 未结 6 1063
名媛妹妹
名媛妹妹 2020-12-13 08:12

I\'m using sorcery for authentication along with twitter bootstrap.

I\'d like to style my error messages on my signup form in the style of twitter\'s bootstrap by ch

6条回答
  •  别那么骄傲
    2020-12-13 08:45

    For Bootstrap 3.2 you could use sth. like this (add nokogiri gem to your Gemfile):

    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

    Place this code inside config/initializers/field_error_proc.rb file (create one if not exists)

    This is slightly modified code from: Overriding ActionView::Base.field_error_proc in Rails

提交回复
热议问题