0

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

j-zhang
  • 693
  • 1
  • 8
  • 17
  • Yes you ca use `twitter-bootstrap-rails` to apply custom bootstrap for your form check this link: https://github.com/seyhunak/twitter-bootstrap-rails – Choco Nov 21 '14 at 02:42
  • @Choco Thank you very much. I know that gem. I don't know how to check error and use the error style's html source. – j-zhang Nov 21 '14 at 02:49
  • please refer these links https://github.com/bootstrap-ruby/rails-bootstrap-forms and http://stackoverflow.com/questions/15155890/styling-form-error-message-bootstrap-rails they may help you – Choco Nov 21 '14 at 03:22
  • @Choco Very good! I think that's what I want. Lots of thanks! If you can write a answer blow I will accept it:) – j-zhang Nov 21 '14 at 03:36
  • I posted my answer Thanks – Choco Nov 21 '14 at 03:42

1 Answers1

1

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 %>
Community
  • 1
  • 1
Choco
  • 1,054
  • 1
  • 7
  • 20
  • Will this rails-bootstrap-forms play well with twitter-bootstrap-rails? I already have twitter-bootstrap-rails, can I just drop rails-bootstrap-forms into the same app for form styling? – Anand Nov 21 '14 at 05:26
  • 1
    @anand I think it will work as both those gems are different and having different styles – Choco Nov 21 '14 at 05:28
  • A wonderful answer! Very helpful. @Anand I am using the both two gems now, and there is no problem until now. – j-zhang Nov 21 '14 at 05:36