arvindvyas / Country-State-Select

It will fetch the countries and according to that fetch the state of that countries, currently it is for countries listing
MIT License
69 stars 78 forks source link

Feature to default select options #39

Open arvindvyas opened 8 years ago

arvindvyas commented 8 years ago

this is patch


   <%= f.input :test_country, label: "Country", collection: CountryStateSelect.countries_collection, selected:CountryStateSelect.countries_collection[229] %>

    <%= f.input :test_state, collection: CS.states(:us).values, selected: CS.states(:us)[:FL] %>

Looking for method which we can set by chosen itself

joseh-henrique commented 7 years ago

Using this way it values ​​are frozen, I have to start by the country and state so I select another city for example, how to do with javascript this correctly?

Attempts below, no result :-(

First try _form.html.erb

<%= f.hidden_field :leader_id %>
<%= f.input :country_code, label: 'País',
            collection: CountryStateSelect.countries_collection,
            selected: CountryStateSelect
                .countries_collection
                .select{|_nome,sigla| sigla == f.object.leader.country_code.to_sym}[0][1] %>
<%= f.input :subregion_code,
            field_names: {country: :country_code, state: :subregion_code},
            collection: CS.states(f.object.leader.country_code.to_sym).values,
            selected: CS.states(f.object.leader.country_code.to_sym)[f.object.leader.subregion_code.to_sym] %>
<%= f.input :city,
            field_names: {state: :subregion_code, city: :city},
            collection: CS.cities(f.object.leader.subregion_code.to_sym, f.object.leader.country_code.to_sym),
            selected: CS.cities(f.object.leader.subregion_code.to_sym, f.object.leader.country_code.to_sym)
                .select{ |x| x.to_s == f.object.leader.city.to_s } %>

Second attempt enrolments_controller.rb

def new_participant
  @user = @enrolment.subscribers.build({
        president: @enrolment.president,
        country_code: @enrolment.country_code,
        subregion_code: @enrolment.subregion_code,
        city: @enrolment.city})
end

_form.html.erb

<%= f.input :country_code, collection: CountryStateSelect.countries_collection %>
<%= f.input :subregion_code, CountryStateSelect.state_options(
            form: f,
            field_names: {country: :country_code, state: :subregion_code}) %>
<%= f.input :city, CountryStateSelect.city_options(
           form: f,
           field_names: {state: :subregion_code, city: :city}) %>

Nada. Subregion_code and city freeze, I must change the country to work if I have to change the city. :-(

jondkinney commented 5 years ago

Here's what worked for me. It's in the context of a search for users, so it's not a form for an existing record. That being the case I was able to initialize a user object with the proper country_field details to make the default country selection and proper states (states from the US in my case) load by default on a fresh page render before any JS is executed.

= simple_form_for User.new(country_field: (params[:user][:country_field] rescue 'US')), url: search_index_path, method: :get do |f|
  %ol
    %li
      = f.input :country_field, collection: CountryStateSelect.countries_collection, prompt: 'Select Country', label: 'Country', input_html: { class: 'selectize' }
    %li
      - state_options = { form: f, field_names: { country: :country_field, state: :state_field } }
      = f.input :state_field, CountryStateSelect.state_options(state_options).merge(label: 'State/Province', selected: (params[:user][:state_field] rescue ''), prompt: 'Select State/Province')