Last Updated: April 14, 2019
·
5.952K
· julianduque

Custom error partial with Ruby on Rails and Twitter Bootstrap

When scaffolding with Twitter Bootstrap in Rails (See: https://github.com/seyhunak/twitter-bootstrap-rails) the validation errors aren't showing by default so I use a partial like the following:

<% if object.errors.any? %>
  <div class="alert alert-error">
    <a class="close" data-dismiss="alert">&#215;</a>
    <ul>
      <% object.errors.full_messages.each do |msg| %>
        <%= content_tag :li, msg, :id => "error_#{msg}" if msg.is_a?(String) %>
      <% end %>
    </ul>
  </div>
<% end %>

Then in my _form.html.erb file I embed the partial like this

<%= render 'layout/errors', object: @model_object %>

I hope this help :)

This is the gist of the partial: https://gist.github.com/3184416

2 Responses
Add your response

Adopted to reflect changes made in bootstrap3:

<% if object.errors.any? %>
  <div class="alert fade in alert-danger alert-dismissable"><button aria-hidden="true" class="close" data-dismiss="alert" type="button">×</button>
    <ul>
      <% object.errors.full_messages.each do |msg| %>
        <%= content_tag :li, msg, :id => "error_#{msg}" if msg.is_a?(String) %>
      <% end %>
    </ul>
  </div>
<% end %>
over 1 year ago ·

nice and useful!

over 1 year ago ·