Show error in field instead of flash / notice

1

I have a Rails application with Twitter Bootstrap that, when creating a record and displaying errors, shows the error in a flash / notice as the image:

However, I would like errors to appear in the fields, and change the input class to .has-error or something. I took a look at the field_error_proc and tried to change it in several ways, however, without success (actually, maybe I did something wrong).

    
asked by anonymous 11.09.2014 / 22:05

2 answers

1

Using form helpers

<%= form_for @post |f| %>
  <% @post.errors[:title].join(", ") %>
  <%= f.text_field :title %>
  <%= f.submit "Create" %>
<% end %>

Error fields have an output like this:

<div class="field_with_errors">
 <input id="post_title" name="post[title]" size="30" type="text" value="">
</div>

using class field_with_errors :

.field_with_errors {
  padding: 2px;
  background-color: red;
  display: table;
}

source

    
12.09.2014 / 02:18
1

In the case of Bootstrap, you can make the border of fields with red error with a simple line in jQuery:

$(".form-group:has(.field_with_errors)").addClass("has-error");
    
12.09.2014 / 02:19