Ruby on Rails和引导,使field_with_errors显示水平(ruby on ra

2019-10-18 12:56发布

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>

Answer 1:

由于您使用的形式内联和DIV显示为一个块默认。 尝试

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

    display: inline-block;
}


Answer 2:

需要注意的是与引导3个名称已更改

.field_with_errors {
  @extend .has-error;
  display: inline-block;
}


Answer 3:

我最终使用下列内容:

.field_with_errors {
  @extend .has-error;
}

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

这延长了.has-error为各种形式的Bootstrap类,并且如果使用的形式是一个内联形式( form-inline )时,它显示.field_with_errors作为inline-block



文章来源: ruby on rails and bootstrap , make field_with_errors display horizontal