0

我的一个 Rails 表单中有一个字段,它选择了一个“单元”并且最初可以正常工作。但是,当我进入记录时,选择默认为另一个单位,而不是分配的单位,因此不保留该值。

_form.html.erb
<%= f.select :unit_id, options_for_select(unit_select), {}, {:class => 'select'} %>  

helper
def unit_select
    Unit.all.map{|unit| unit.calls.empty? ? [unit.unit_name, unit.id] : ["#{unit.unit_name} (on call)", unit.id] }
  end

我唯一能想到的是它没有保留值,因为表单字段正在使用地图。我对 Rails 和 Ruby 有点陌生,所以我还没有完全理解我的代码的约束。

任何帮助或建议表示赞赏。

4

1 回答 1

1

我能够通过使用 options_for_select 来解决这个问题:selected => @call.unit.id

然后它保留了先前选择的值。

于 2012-08-07T16:42:50.700 回答