简体   繁体   English

在date_field上的Bootstrap on rails form helper问题并选择

[英]Bootstrap on rails form helper issue with date_field and select

I'm trying to style my form in rails with bootstrap-4. 我正在尝试使用bootstrap-4在表单中设置表单的样式。 But I'm having issues apply styles to the date_field helper and the select helper. 但是我在将样式应用于date_field助手和选择助手时遇到了问题。

This is the date field. 这是日期字段。 The class won't apply and throws an error until I take it off. 直到我将其取消,该课程才适用,并引发错误。

<div class="form-group row">
    <%= form.label :appointment, :class => 'col-md-3 col-form-label text-md-right' %>
    <div class="col-md-9">
         <%= date_field(:patient, :apointment), :class => 'form-control' %>
    </div>
</div>

This is the select helper. 这是选择助手。 The options are appearing outside of the select box. 选项显示在选择框之外。

<div class="form-group row">
              <%= form.label :consultationType, :class => "col-md-3 col-form-label text-md-right" %>
            <div class="col-md-9">
                <select class ="form-control">
                  <!--Gets all counties from DB -->
                  <%= form.select :consultationType, 
                [
                    "N/A",
                    "Inhouse-Clinic",
                    "St.James Hospital - X-Ray",
                    "Matter Private Dublin - Cardiology",
                    "Matter Private Cork - Neurology",
                    "Royal Eye and Ear - Ophthalmology",
                    "Temple Street - Children"
                ] 
            %>
                </select>

        </div>
      </div>

Try to the following 尝试以下

<%= date_field(:patient, :apointment, class: "form-control") %>

Generated HTML 生成的HTML

<input class="form-control" type="date" name="patient[apointment]" id="patient_apointment">

And for the select tag, you have declared two times, you can remove the HTML <select> tag and then add the bootstrap class like this 对于select标记,您已经声明了两次,可以删除HTML <select>标记,然后添加如下所示的bootstrap类

<%= form.select :consultationType,
    [
        "N/A",
        "Inhouse-Clinic",
        "St.James Hospital - X-Ray",
        "Matter Private Dublin - Cardiology",
        "Matter Private Cork - Neurology",
        "Royal Eye and Ear - Ophthalmology",
        "Temple Street - Children"
    ], {}, {class: "form-control"} 
%>

Generated HTML like this 这样生成的HTML

<select class="form-control" name="patient[consultationType]" id="model_consultationType"><option value="N/A">N/A</option>
    <option value="Inhouse-Clinic">Inhouse-Clinic</option>
    <option value="St.James Hospital - X-Ray">St.James Hospital - X-Ray</option>
    <option value="Matter Private Dublin - Cardiology">Matter Private Dublin - Cardiology</option>
    <option value="Matter Private Cork - Neurology">Matter Private Cork - Neurology</option>
    <option value="Royal Eye and Ear - Ophthalmology">Royal Eye and Ear - Ophthalmology</option>
    <option value="Temple Street - Children">Temple Street - Children</option>
</select>

For the date_field move the class inside the brackets: 对于date_field请将类移到方括号内:

date_field(:patient, :appointment, class: 'form-control')

And for the select it is not required to give HTML select tag explicitly. 对于select ,不需要显式提供HTML select标签。 Just doing like this will do the job: 只需执行以下操作即可:

<%= form.select :consultationType, ["N/A", "Inhouse-Clinic", "St.James Hospital - X-Ray", "Matter Private Dublin - Cardiology", "Matter Private Cork - Neurology", "Royal Eye and Ear - Ophthalmology", "Temple Street - Children"], {}, { class: 'form-control' }  %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM