Faw*_*wyd 19 forms ruby-on-rails-3 twitter-bootstrap
我的rails表单的错误消息看起来像带引导程序的狗屎.有没有人知道更好(漂亮的)错误消息的解决方案?我使用Rails和Bootstrap.
我的表格(它是帮手)是这样的:
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-inline">
<%= f.text_field :email, class:'input-large', placeholder:'Test' %>
<!-- </div>
<div class="actions"> -->
<%= f.submit class:'btn btn-large btn-success' %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

cri*_*ken 23
看看Michael Hartl如何在railstutorial中做到这一点. 
这就是用过的css:
#error_explanation {
color: #f00;
ul {
list-style: none;
margin: 0 0 18px 0;
}
}
.field_with_errors {
@extend .control-group;
@extend .error;
}
Run Code Online (Sandbox Code Playgroud)
他在这里描述了一切.
如果您还希望每行开头都有小星星,则必须将其包含在您的表单中:
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li> * <%= msg %></li> <--- insert here
<% end %>
</ul>
</div>
...
Run Code Online (Sandbox Code Playgroud)
Rab*_*ott 12
我意识到有点晚了,但是我今天刚刚使用Rails 4和Bootstrap 3遇到了这个问题,最后我使用面板创建了一个帮助器来显示错误:
Rails 4/Bootstrap 3
def errors_for(object)
if object.errors.any?
content_tag(:div, class: "panel panel-danger") do
concat(content_tag(:div, class: "panel-heading") do
concat(content_tag(:h4, class: "panel-title") do
concat "#{pluralize(object.errors.count, "error")} prohibited this #{object.class.name.downcase} from being saved:"
end)
end)
concat(content_tag(:div, class: "panel-body") do
concat(content_tag(:ul) do
object.errors.full_messages.each do |msg|
concat content_tag(:li, msg)
end
end)
end)
end
end
end
Run Code Online (Sandbox Code Playgroud)
Rails 4/Bootstrap 4 Beta
def errors_for(object)
if object.errors.any?
content_tag(:div, class: "card border-danger") do
concat(content_tag(:div, class: "card-header bg-danger text-white") do
concat "#{pluralize(object.errors.count, "error")} prohibited this #{object.class.name.downcase} from being saved:"
end)
concat(content_tag(:div, class: "card-body") do
concat(content_tag(:ul, class: 'mb-0') do
object.errors.full_messages.each do |msg|
concat content_tag(:li, msg)
end
end)
end)
end
end
end
Run Code Online (Sandbox Code Playgroud)
Rails 4/Bootstrap 4 Beta列表组变体
def errors_for(object)
if object.errors.any?
content_tag(:div, class: "card border-danger") do
concat(content_tag(:div, class: "card-header bg-danger text-white") do
concat "#{pluralize(object.errors.count, "error")} prohibited this #{object.class.name.downcase} from being saved:"
end)
concat(content_tag(:ul, class: 'mb-0 list-group list-group-flush') do
object.errors.full_messages.each do |msg|
concat content_tag(:li, msg, class: 'list-group-item')
end
end)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我把它放在application_helper中并在表单视图中调用它
<%= errors_for(@user) %>
Run Code Online (Sandbox Code Playgroud)
也许有人会偶然发现它并发现它很有用.
以防万一有人在这里绊倒并Bootstrap 4 alpha使用rails 5and bootstrap_form_for gem。我用:
<div class="form-group">
<%= f.alert_message "Please fix the errors below." %>
</div>
Run Code Online (Sandbox Code Playgroud)
看起来真的很不错。