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">×</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
Written by Julián Duque
Related protips
2 Responses
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#