Customize Form Error Messages in Rails -
so, i'm trying come way customize error messages in form_for
's. elegant way this, in opinion, inside text area itself. far, method i've tried messes form completely. please share thoughts , methods. simple form:
<%= f.label :name, 'name' %><br> <%= f.text_field :name, size: 30 %> <%= f.label :password, 'password' %><br> <%= f.password_field :password, size: 30 %> <%= f.label :password_confirmation, 'confirm' %><br> <%= f.password_field :password_confirmation, size: 30 %> <%= f.submit %>
as can see, no errors mentioned here since i've added initializer follows:
actionview::base.field_error_proc = proc.new |html_tag, instance| errors = array(instance.error_message).join(',') if html_tag =~ /^<label/ html_tag else %(#{html_tag}<span class="validation-error"> #{errors}</span>).html_safe end end
use helper method
def errors_for(model, attribute) if model.errors[attribute].present? content_tag :span, :class => 'error_explanation' model.errors[attribute].join(", ") end end end
and in view:
<%= lesson_form.text_field :description %><br /> <%= errors_for @lesson, :description %>
Comments
Post a Comment