Question

I am using carmen-rails for state dropdown. The state drop down dynamically updates when country is updated but on edit page, the state drop down is not defaulting to the saved state value. Can anyone help?

FYI - I am passing the saved state value as locals parameter u

<div id="order_state_code_wrapper">
  <% parent_region ||= params[:parent_region] %>
  <% if parent_region.nil? %>
    <em>Please select a country above</em>
  <% else %>
    <% country = Carmen::Country.coded(parent_region) %>
    <% if country.nil? %>
       <em>Please select a country above</em>
    <% elsif country.subregions? %>
       <%= subregion_select(:user, :subregion_iso2, parent_region) %>
    <% else %>
       <%= text_field(:user, :subregion_iso2) %>
    <% end %>
  <% end %>
</div>

No correct solution

OTHER TIPS

I ran into the same issue and resolved it by doing the following (pointed in the right direction by https://github.com/jim/carmen-rails/issues/12). It's not yet optimal, but it does the job.

<div id="shipping_tally_state_code_wrapper">
<div class="control-group carmen_state required">
  <label class="carmen_state_code required control-label" for="shipping_tally_state_code"><abbr title="required">*</abbr> Province</label>
    <div class="controls">
      <% parent_region ||= params[:parent_region] %>
      <% sub_region ||= params[:sub_region] %>
      <% country = Carmen::Country.coded(parent_region) %>
      <% if country.nil? %>
        <span class="help-inline display-id"><em>Please select a country above</em></span>
      <% elsif country.subregions? %>
      <%= subregion_select_tag('shipping_tally[state_code]', sub_region, country) %>
      <% else %>
      <%= text_field_tag('shipping_tally[state_code]', sub_region) %>
      <% end %>
    </div>
</div>
</div>

And then call this partial with the following:

<%= render partial: 'shared/forms/subregion_select', :locals => { :parent_region => f.object.country_code, :sub_region => f.object.state_code } %>

Applicable js.coffee no change.

Areas to be improved:

  1. Using twitter bootstrap so hard coding everything instead of putting together a wrapper.
  2. Currently doesn't show validation errors for state_code (a required property).
  3. Doesn't retain the state code when switching the country. This would be easy enough to implement by passing the sub_region to the service along with the parent_region.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top