Castielle
Castielle

Reputation: 435

Make country default in form as "United States"

I am trying to make the default value for the country input in a form to be "United States". I am using the simple form gem and country select gem.

In the simple form initializer, I set this default

  config.country_priority = [ "United States" ]

Which makes the topmost value correct as United States, but the default value is 'Uganda'.

In the _form view, I did the following:

<%= f.input :country, :default => [ 'United States' ] %>

I restarted Rails and it still defaulted to Uganda.

I also tried the following:

<%= f.input :country, :default => 'United States' %>

and

<%= f.input :country, :selected => 'United States' %>

What should I try next?

Upvotes: 7

Views: 3660

Answers (7)

wuworkshop
wuworkshop

Reputation: 71

I know this is an old thread, but I just recently did this and thought I'd share to help others. I'm using Rails 4.2.0, country_select (2.1.1), and simple_form (3.1.0). This worked for me:

<%= f.input :country, selected: 'US' %>

If you want to prevent the user from changing the selection, use :disabled like so:

<%= f.input :country, selected: 'US', disabled: true %>

Just to clarify the :priority option in the other answers, that only gives that country higher priority (i.e. it will place the country at the top of the dropdown list), it doesn't actually select that country. Hope this is helpful.

Upvotes: 3

Deepak
Deepak

Reputation: 361

You need to use the country code instead of the country name:

<%= form.country_select :country, ["US"] %>

Upvotes: 2

Kannan S
Kannan S

Reputation: 2489

I tried this with gem 'country_select' in simple-form. It's worked for me.

= f.input :country,  priority: [ "Brazil" ]

Upvotes: 4

Impact Mobilizer
Impact Mobilizer

Reputation: 11

Am using this and it works. Using formtastic gem, add below line.

          <% f.input :address_country, :as => :country, :priority_countries => ["United States", "United Kingdom","Australia", "New Zealand"] %>         

Note: The countries inside the array are the priority countries which are displayed first before the entire global countries.

Upvotes: 1

Castielle
Castielle

Reputation: 435

I ended up not using the gem -- too much complexity. I just created a constant COUNTRIES and used that instead.

Upvotes: 0

shweta
shweta

Reputation: 8169

Try this,

<%= form.country_select :country, ["United States"] %>

Upvotes: 4

Jay Truluck
Jay Truluck

Reputation: 1519

You should only have to do:

<%= f.country_select("user", "country_name", [ "United States" ]) %>

Where user is the name of the model you are using.

Upvotes: -1

Related Questions