【发布时间】:2023-03-25 01:57:01
【问题描述】:
如果我使用 Rails 4 进行测试:
rails g scaffold post title description:text
这里会生成表单源:
<%= 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 %>
还有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;
}
}
结果是:
但是如果我想使用twitter bootstrap的表单错误类,比如:
如何以正确的方式做到这一点?有不错的宝石出口吗?
确实,我想要这种风格:
【问题讨论】:
-
是的,您可以使用
twitter-bootstrap-rails为您的表单应用自定义引导程序检查此链接:github.com/seyhunak/twitter-bootstrap-rails -
@Choco 非常感谢。我知道那颗宝石。我不知道如何检查错误并使用错误样式的html源。
-
@Choco 很好!我想这就是我想要的。非常感谢!如果你能写一个答案,我会接受它:)
-
我发布了我的答案谢谢
标签: css ruby-on-rails forms twitter-bootstrap scaffold