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
How to Check If a Ruby Array Includes One of Several Values
Putting the Results of Pp (Or Anything Outputted to Console) into a String
How to Send Message Using Gmail API with Ruby Google API Client
What Does "||=" Do in Ruby 1.9.2
Convert (Decode) Hexadecimal String to Binary String
Ruby Forgets Local Variables During a While Loop
Disable Sprockets Asset Caching in Development
How to Insert a String into a Textfile
How to Fire Raw Mongodb Queries Directly in Ruby
How to Install Redcloth on Windows
Has Anyone Successfully Deployed a Rails Project with Ruby 1.9.1
Activerecord::Connectiontimeouterror
Rails Active Admin CSS Conflicting with Twitter Bootstrap CSS
Connect to a Locally Built Jekyll Server Using Mobile Devices in the Lan