ruby on rails - Custom HTML Error Wrappers for Form Elements -
i find way customize default error html
<div class="field_with_errors"></div>
to take own classes:
<div class="clearfix error"> <label for="errorinput">input error</label> <div class="input"> <input class="xlarge error" id="errorinput" name="errorinput" size="30" type="text"> <span class="help-inline">small snippet of text</span> </div> </div>
i have found railscast 2007 uses rails 2, believe. http://railscasts.com/episodes/39-customize-field-error. seems rails 3 might have more friendly way customize html?
also, doesn't show way add error class directly input want.
the method explained in link posted still used today vanilla form builders in rails.
so, if wanted wrap input mention, create method overriding actionview::base.field_error_proc
in environment.rb
file example, so:
actionview::base.field_error_proc = proc.new |html_tag, instance| if instance.error_message.kind_of?(array) %(<div class="form-field error">#{html_tag}<small class="error"> #{instance.error_message.join(',')}</small></div).html_safe else %(<div class="form-field error">#{html_tag}<small class="error"> #{instance.error_message}</small></div).html_safe end end
in above code, i'm wrapping input (the #{html_tag}) in <div class="form-field error>..</div>
that's default used zurb foundation. i'm using <small class="error">...</small
tag (which foundation default) display messages below input.
however, recommend using form builder gem simple_form. cleans view code quit bit , allows level of customization require.
check out railscast on here.
good luck!
Comments
Post a Comment