Muhammed Bhikha
Muhammed Bhikha

Reputation: 5019

Rails : form select helper to have a starting default

I have a simple form. Also as part of the form I have a variable with an array/list of all the country names. The form picks up the list just fine, however, it starts off on the first value, I think its Afghanistan, however I want it to start/default with United Kingdom

This is my code for the form select for the countries.

<%= f.select(:country, @country_list.map { |value| [ value, value ] }) %>

I have tried without success:

<%= f.select(:country, @country_list.map { |value| [ value, value ] },['United Kingdom']) %>

Upvotes: 4

Views: 3382

Answers (2)

Thilo
Thilo

Reputation: 17735

From the documentation:

Specify :selected => value to use a different selection or :selected => nil to leave all options unselected.

So if you want the default to be selected, try this:

f.select(:country, @country_list.map { |value| [ value, value ] }, selected: "United Kingdom")

Upvotes: 2

Muhamamd Awais
Muhamamd Awais

Reputation: 2385

Have you tried selected option

:selected =>  "United Kingdom" or your_country_id

not sure about the syntax but hopefully it would work

Upvotes: 3

Related Questions