forms - Why am I unable to override Rails' rendering elements with field_with_errors class? -


i'm told default, if error occurs in model, rails renders form element set so

  <div class="field">     <%= f.label :price, 'price', :class => "control-label" %> <span class="required">*</span> <br>     <%= f.text_field :price, :size => 30 %>     <div class='error'><%= show_errors(@user_event, :price) %></div>   </div> 

as

<div class="field">     <div class="field_with_errors"><label class="control-label" for="user_event_price">price</label></div> <span class="required">*</span> <br>     <div class="field_with_errors"><input size="30" value="" name="user_event[price]" id="user_event_price" type="text"></div>     <div class="error">please enter value price.</div>   </div> 

so said should change config/environment.rb , override default behavior. don't want

<div class="field_with_errors"> 

element appearing. set file so

# load rails application. require_relative 'application'  # initialize rails application. rails.application.initialize!  actionview::base.field_error_proc = proc.new |html_tag, instance|   if instance.error_message.kind_of?(array)     %(<div class="form-field error3">#{html_tag}<small class="error">&nbsp;       #{instance.error_message.join(',')}</small></div).html_safe   else     %(<div class="form-field error3">#{html_tag}<small class="error">&nbsp;       #{instance.error_message}</small></div).html_safe   end end 

but still element renders "field_with_errors" class. doing incorrectly above? want element out of there!

edit: here's output in response gokul's answer

<div class="field">     <div class="form-field error3"><label class="control-label" for="user_event_price">price</label><small class="error">&nbsp;       please enter value price.</small></div>* <br>     <div class="form-field error3"><input size="30" value="" name="user_event[price]" id="user_event_price" type="text"><small class="error">&nbsp;       please enter value price.</small></div>please enter value price.</div> 

create initializer following code:

/config/initializers/format_errors.rb

actionview::base.field_error_proc = proc.new |html_tag, instance|   html_tag.html_safe end 

and, don't forgot restart server :-)


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -