简体   繁体   中英

formtastic removing hidden fields (day/month) in date_select

I'm using Formtastic in Rails 4.1

I have set the date input order: [:year] as I only want to capture the year, and don't care about the month and day. I am aware that you can use discard_day: true and discard_month: true as well but adding those options doesn't seem to make any difference.

The problem is that in my generated HTML, the month and date fields are hidden (as I want them to be), but still take up a large amount of space in their Bootstrap column. I want them hidden and also to not take up any space, as they are messing up my formatting.

#erb
<div class="row">
  <%= f.inputs do %>
    <div class="form-group nested-fields">
      <div class="col-md-2">
        <%= f.input :date, order: [:year], start_year: Time.now.year - 100, end_year: Time.now.year %>
      </div>
      <!-- ./col-md-2 -->

      <div class="col-md-8">
        <%= f.input :description %>
      </div>
      <!-- ./col-md-8 -->

      <div class="col-md-2 fields">
        <%= link_to_remove_association "Remove previous operation", f %>
      </div>
      <!-- ./col-md-2 -->

    </div>
    <!-- ./form-group -->

  <% end %>
  <%# f.inputs %>
</div>
<!-- ./row -->

#Rendered HTML
<div class="row">
  <div id="previous_operations">

    <div class="row">
      <fieldset class="inputs">
        <div class="form-group nested-fields">
          <div class="col-md-4">
            <div class="date_select input optional form-group"id="patient_previous_operations_attributes_0_date_input"><label class="  control-label" for="patient_previous_operations_attributes_0_date">Date</label>
              <span class="form-wrapper">
                <input id="patient_previous_operations_attributes_0_date_2i" name="patient[previous_operations_attributes][0][date(2i)]" type="hidden" value="1" />
                <input id="patient_previous_operations_attributes_0_date_3i" name="patient[previous_operations_attributes][0][date(3i)]" type="hidden" value="1" />
                <div class="row">
                  <div class="col-xs-4"><select class="form-control" id="patient_previous_operations_attributes_0_date_1i" name="patient[previous_operations_attributes][0][date(1i)]" placeholder=".col-xs-4">
                      <option value=""></option>
                      ...
                      <option value="2014">2014</option>
                    </select>
                  </div>
                </div>
              </span>
            </div>
          </div>
          <!-- ./col-md-4 -->

Note especially these lines

            <input id="patient_previous_operations_attributes_0_date_2i" name="patient[previous_operations_attributes][0][date(2i)]" type="hidden" value="1" />
            <input id="patient_previous_operations_attributes_0_date_3i" name="patient[previous_operations_attributes][0][date(3i)]" type="hidden" value="1" />
            <div class="row">
              <div class="col-xs-4"><select class="form-control" id="patient_previous_operations_attributes_0_date_1i" name="patient[previous_operations_attributes][0][date(1i)]" placeholder=".col-xs-4">

The first two (date_2i and date 2i inputs) are the month and day fields being hidden as desired, but still taking up a huge block of the screen.

The latter (date_1i) is the year select that I want to take up the space that all three are currently occupying (two are invisible). For some reason formtastic adds the "col-xs-4" class to the year control, which I think is the problem, and I don't know why or how to switch it off.

Help appreciated.

您可以使用以下方法隐藏这些字段:

discard_hour: true, discard_minute: true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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