1

custom.css.scss文件中

.field_with_errors {
    @extend .control-group;
    @extend .error;

}

和 html.erb

<%= form_for @timecard , url:{action: "savecard"},html:{class: "form-inline"} do |f| %>
    <%= f.label :"Date"%>
    <%= f.date_select :starttime ,{use_month_numbers: true }, class: "input-small" %>
    <%= f.label :"Hours"%>
    <%= f.number_field :hours, class: "input-small" %>
    <%= f.label :"Project"%>
    <% projects = Project.all.map { |project| [project.name, project.charge_number]  } %>
    <%= f.select :charge_number, options_for_select(projects) ,{},class: "input-small" , style:"width:150px"%>
    <%= f.submit "Save", class: "btn" %>
    <%= f.submit "Delete", class: "btn" %>
    <%= f.submit "Submit", class: "btn btn-danger",confirm:"You can't edit it agin if you've submitted it" %>
<% end %>

在正常状态下,它看起来不错

在此处输入图像描述 但是如果发生了错误,它看起来像这样 在此处输入图像描述

以及它生成的 html 代码

<label for="time_card_Hours">Hours</label>
    <div class="field_with_errors"><input class="input-small" id="time_card_hours" name="time_card[hours]" type="number" /></div>
    <label for="time_card_Project">Project</label>
4

3 回答 3

4

因为您使用的是内联表单,并且 div 显示为块默认值。尝试

.field_with_errors {
    @extend .control-group;
    @extend .error;

    display: inline-block;
}
于 2013-08-19T06:24:19.867 回答
4

请注意,使用 Bootstrap 3名称已更改

.field_with_errors {
  @extend .has-error;
  display: inline-block;
}
于 2015-06-02T13:35:55.797 回答
3

我最终使用了以下内容:

.field_with_errors {
  @extend .has-error;
}

.form-inline .field_with_errors {
  display: inline-block;
}

这为所有表单扩展了.has-errorBootstrap 类,如果使用的表单是内联表单 (. form-inline),则显示.field_with_errorsinline-block.

于 2017-06-09T19:48:19.570 回答