Harsha M V
Harsha M V

Reputation: 54949

Rails select - default selected value error

I am trying to set the selected attribute for select but it does not seem to be working

<%= 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 %>

Even by forcing the value i am not able to get it selected

Updated from the Answers

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

But am not able to access the hash value of min to the selected value

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

Upvotes: 0

Views: 64

Answers (1)

DanneManne
DanneManne

Reputation: 21180

You can supply it as a second argument to the helper method options_for_select, for example:

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

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

Upvotes: 1

Related Questions