mardi 12 janvier 2016

Rails Drop Down List: How to Change to Text Field Based on Selection on Another Drop Down List

In my Rails Application I have a Country Select Option Drop down list using the country_select gem,and next to the country select drop down there is another dropdown to display the states in "US" like the code bellow:

CODE OF COUNTRY SELECT DROP DOWN

<div class="row">
    <div class="form-group">
      <%= f.label :country,'Country', class:"control-label"%>
      <%= f.country_select(:country ,  {priority_countries: [ "US"]} ,{class: "form-control"}) %>
    </div>
</div>

CODE OF STATE SELECT DROP DOWN

      <%= f.label :state,'State', class:"control-label" , style: "color: black;"%>
      <%= f.select( :state, options_for_select(us_states), {}, {class: "form-control"}) %>

Here us_states method is defined in application_helper.rb file like bellow:

def us_states
    [
        ['Alabama', 'AL'],
        ['Alaska', 'AK'],
        ['Arizona', 'AZ'],
     ...............etc
    ]

  end

Here the default selected country is "US", but if the user select any other country from the country select drop down then I want make the state select drop down into a normal text field not a drop down list. How can I make the state select drop down into a text field if the user select a country other than US ? And if the selected country is US the state section must stay as a drop down ?

Aucun commentaire:

Enregistrer un commentaire