Rails Select Drop Down for States

Display us states dropdown on view in rails

May be:

us_states.select{|v| v[1]=="WA"}.first[0]

Rails form select dropdown show the value stored in the database

The problem is when using options_for_select with select, the selected option should be included as an argument for options_for_select. Also you should use prompt instead of include_blank So your code should look like below

<%= f.select(:state, options_for_select(us_states, selected: @clinician_profile.state), prompt: "Please Select") %>

Drop-down for country-states

Try the Carmen plugin:

http://autonomousmachine.com/posts/2009/4/1/carmen-a-rails-plugin-for-geographic-names-and-abbreviations

You can also check this other stackoverflow post:

Ruby on Rails Country/State Select Enigma

Rails Cascading Drop Down List: Change Drop down menu into Text Field based on Previous Drop Down Option Selected

I would recommend to add both Text Field and State dropdown initially and toggle them depending on Country value.

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

Now add toggle logic in jquery

<script type='text/javascript'>
$("#country_drop_down_id").change(function(){
if($(this).val()=='US'){
$('#state_drop_down_id').show();
$('#state_text_field_id').hide();
}else{
$('#state_drop_down_id').hide();
$('#state_text_field_id').show();
}
});
</script>

Replace
country_drop_down_id , state_drop_down_id , state_text_field_id
with corresponding ID selector.

Rails : Drop down in simple_form doesn't retain value when directed to edit path

You can using selected, like:

options_for_select(us_states, selected: "set_current_value")

More usage examples - options_for_select() docs.



Related Topics



Leave a reply



Submit