Rails选择 - 默认选择值错误

时间:2016-04-26 08:54:41

标签: ruby-on-rails ruby-on-rails-4 select

我正在尝试为select设置所选属性,但它似乎无法正常工作

<%= f.fields_for :age_range, OpenStruct.new(@campaign.age_range) do |d| %>

              <div class="form-group">
                  <%= d.label :min, "Age Range", :class => 'col-sm-2 control-label' %>
                  <div class="col-sm-2">
                    <%= d.text_field :min, :class => 'form-control' %>
                    <%= d.select :min, options_for_select(13..90), { include_blank: "Min Age", selected: '23' }, { class: 'form-control'} %>
                  </div>
                  <div class="col-sm-2">
                    <%= d.text_field :max, :class => 'form-control' %>
                    <%= d.select :max, options_for_select(13..90), { include_blank: "Max Age", selected: '23' }, { class: 'form-control'} %>
                  </div>
              </div>
              <div class="line line-dashed b-b line-lg pull-in"></div>

            <% end %>

即使通过强制价值,我也无法将其选中

从答案更新

<%= d.select :min, options_for_select((13..90), @campaign.age_range[:min]), { include_blank: "Min Age" }, { class: 'form-control'} %>

但是我无法访问min的哈希值到所选值

@campaign[:age_range] is a hash in the database

1 个答案:

答案 0 :(得分:1)

您可以将其作为辅助方法options_for_select的第二个参数提供,例如:

# Static number
options_for_select(13..90, 13)

# or retrieved somehow from form object
options_for_select(13..90, d.object.min)