1

我有一个带有输入字段和下拉框的表单,其中包含来自其他表的数据。(加入)但现在当我提交时很好,但是当我想编辑字段时,下拉字段为空但文本字段不是

我的表单代码:

<%= form_for @contracten, :html => { :multipart => true } do |f| %>
  <% if @contracten.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@contracten.errors.count, "error") %> prohibited this contracten from being saved:</h2>

      <ul>
      <% @contracten.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div id="forms">
  <div class="field">
    <%= f.label :naam %><br />
    <%= f.text_field :naam %>
  </div>
  <br>
  <div class="field">
    <%= f.label :omschrijving %><br />
    <%= f.text_area :omschrijving %>
  </div>
  <br>
  <div class="field">
    <%= f.label :organisatie %><br />
    <%= f.select :organisatieid, options_for_select(@organisaties.map{ |f| [f.naam, f.id] }), :include_blank => true %>
  </div>
  <br>
  <div class="field">
    <%= f.label :vestiging %><br />
    <%= f.select :vestigingid, options_for_select(@vestigingens.map{ |f| [f.naam, f.id] }), :include_blank => true %>
  </div>
  <br>
  <div class="field">
    <%= f.label :Beheerder %><br />
    <%= f.select :persoonid, options_for_select(@personen.map{ |f| [f.naam, f.id] }), :include_blank => true %>
  </div>
  <br>
  <div class="field">
    <%= f.label :contractsoort %><br />
    <%= f.select :contractsoortid, options_for_select(@contractsoorten.map{ |f| [f.naam, f.id] }), :include_blank => true %>
  </div>
  <br>
  <div class="field">
    <%= f.label :datumingang %><br />
    <%= f.datetime_select :datumingang %>
  </div>
  <br>
  <div class="field">
    <%= f.label :datumeinde %><br />
    <%= f.datetime_select :datumeinde %>
  </div>
  <br>
  <div class="field">
    <%= f.label :contractduur %><br />
    <%= f.number_field :contractduur, :placeholder => 'in jaren' %>
  </div>
  <br>
  <div class="field">
    <%= f.label :opzegtermijn %><br />
    <%= f.number_field :opzegtermijn, :placeholder => 'In Maanden' %>
  </div>
  <br>
  <div class="field">
    <%= f.label :betalingsperiode_eenheid_id %><br>
    <%= f.select :betalingsperiodeeenheidid, options_for_select(@betalingsperiodeeenhedens.map{ |f| [f.omschrijving, f.id] }), :include_blank => true %>
  </div>
  <br>
  <div class="field">
    <%= f.label :betalingstermijn %><br />
    <%= f.number_field :betalingstermijn %>
  </div>
  <br>
  <div class="field">
    <%= f.label :pdf %><br />
    <%= f.file_field :pdf %>
  </div>
  <br>
  <div class="actions">
    <%= f.submit "Toevoegen", :class => 'button3' %>
  </div>
  </div>
<% end %>
<br>

截屏: 带有下拉菜单和文本区域的表单

我希望有人知道是什么问题。

4

1 回答 1

4

添加:selected => ...到您的options_for_select参数。

例如:

<%= f.select :contractsoortid, 
             options_from_collection_for_select(@contractsoorten, 'id', 'naam',
                                               :selected => @contracten.contractsortid), 
             :include_blank => true %>

更改实际上已保存,但在呈现表单时,必须指定实际选择的值。

于 2012-07-12T11:55:10.460 回答