【问题标题】:Prefill right date from record into a Select Field in Edit view将记录中的正确日期预填充到编辑视图中的选择字段中
【发布时间】:2015-05-27 15:15:26
【问题描述】:

我的表单中有以下字段,当我加载它以输入新记录时,一切正常,开始日期是 2016 年,下降到 1900 年女巫很好。它将正确保存到记录中。但是当我去编辑这条记录时,它每次都会以 2016 年的日期加载它......

我希望它显示已保存到数据库中的任何日期。

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

这也是嵌套形式,可能有几辆汽车,所以不能只使用记录实例变量。

它是这样嵌套的:

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

所有其他字段都可以正常工作...

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>

【问题讨论】:

  • options_for_select((1900..Date.today.year+1).to_a.reverse, value_here_from_object )
  • 是的,但我如何获得该对象的价值......这就是问题

标签: ruby-on-rails forms ruby-on-rails-3


【解决方案1】:

找到答案

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

要获取嵌套对象的值,您可以这样做:

 f.object.year  <<< in my case i needed the year

【讨论】:

    猜你喜欢
    • 2013-12-28
    • 1970-01-01
    • 2016-10-26
    • 2013-06-28
    • 2016-02-05
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多