mercredi 27 mai 2015

Prefill right date from record into a Select Field in Edit view

I have the Following field in my Form and everything work good when i load it up to enter in a new Record starting date is 2016 and goes down to 1900 witch is fine. It will save into the record correctly. but when i go to EDIT this record it loads it up with the date of 2016 every time...

I want it to show whatever date has been saved to the database.

<%= f.select :year, options_for_select((1900..Date.today.year+1).to_a.reverse), {:prompt => 'Year', :class=>"pointer" } %>

Also this is in a nested Form and there could be several vehicles so cant just use the record instance variable.

it is nested like this:

 <%= f.fields_for :vehicles do |builder| %>
     <%= render "shipments/partials/vehicle_fields", :f => builder %>
 <% end %>

All other fields work fine...

vehicle_fields:

<section style="margin-bottom: 0;" class="auto-container fields">
  <div class="row">
    <div class="col col-md-2">
      <label class="select">
        <%= f.select :year, options_for_select((1900..Date.today.year+1).to_a.reverse), {:prompt => 'Year', :class=>"pointer" } %>
      </label>
    </div>
    <div class="col col-md-4">
      <label class="input">
        <%= f.text_field :make, :placeholder => "Make" %>
      </label>
    </div>
    <div class="col col-md-4">
      <label class="input">
        <%= f.text_field :model, :placeholder => "Model" %>
      </label>
    </div>
    <div class="col col-md-2">
      <p class="btn btn-primary pull-right" style="margin: 0 0 0 20px"><%= button_to_remove_fields "Remove", f %></p>
    </div>
  </div>
</section>

Aucun commentaire:

Enregistrer un commentaire