How to show beautiful error messages with twitter bootstrap by rails framework?

2.7k views Asked by At

If I use Rails 4 to have a test:

rails g scaffold post title description:text

There will generate the form source:

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And the scaffold.css.scsss:

body {
  background-color: #fff;
  color: #333;
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: 13px;
  line-height: 18px;
}

p, ol, ul, td {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: 13px;
  line-height: 18px;
}

pre {
  background-color: #eee;
  padding: 10px;
  font-size: 11px;
}

a {
  color: #000;
  &:visited {
    color: #666;
  }
  &:hover {
    color: #fff;
    background-color: #000;
  }
}

div {
  &.field, &.actions {
    margin-bottom: 10px;
  }
}

#notice {
  color: green;
}

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

#error_explanation {
  width: 450px;
  border: 2px solid red;
  padding: 7px;
  padding-bottom: 0;
  margin-bottom: 20px;
  background-color: #f0f0f0;
  h2 {
    text-align: left;
    font-weight: bold;
    padding: 5px 5px 5px 15px;
    font-size: 12px;
    margin: -7px;
    margin-bottom: 0px;
    background-color: #c00;
    color: #fff;
  }
  ul li {
    font-size: 12px;
    list-style: square;
  }
}

The result is:

enter image description here

But if I want to use twitter bootstrap's form error class, such as:

enter image description here

How to do it by a right way? Is there a nice gem exit?

In deed, I want this style:

enter image description here

1

There are 1 answers

3
Choco On BEST ANSWER

You can use rails-bootstrap-forms gem with which we can customize rails forms by applying custom bootstrap styles to our form.

Gemfile:

gem 'bootstrap_form'

application.css.scss:

 *= require rails_bootstrap_forms

Form

<%= bootstrap_form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Please refer these links for further assistance:

https://github.com/bootstrap-ruby/rails-bootstrap-forms

Styling form error message - bootstrap/rails

Hope it helps.


Form should be:

<%= bootstrap_form_for(@question) do |f| %>
  <%= f.alert_message "Please input values." %>
  <%= f.text_field :title %>
  <%= f.text_field :description %>
  <%= f.submit %>
<% end %>