I've been trying to follow this: Why is carmen-rails not defaulting the saved state and GitHub issue: country_select in nested model doesn't get filled when editing record. But I can't seem to get the code working.
I've added sub_region to pass to the partial but I think the problem is my @country value is nil for some reason when I go to the edit form. When I am selecting the country, my @testcountry.inspect dynamically returns the selected country. When creating a new user, the form works fine. After choosing country, the state field dynamically updates to the relevant select dropdown list.
_subregion_select.html.erb
<div id="order_state_code_wrapper">
<% parent_region ||= params[:parent_region] %>
<% sub_region ||= params[:sub_region] %>
<% @testcountry = Carmen::Country.coded(parent_region) %>
<% unless parent_region.nil? %>
<% @country = Carmen::Country.coded(parent_region) %>
<% end%>
<%= @testcountry.inspect %>
<% if @country.nil? %>
<em>Please select a country above</em>
<% elsif @country.subregions? %>
<%= subregion_select(:user, :subdivision, parent_region) %>
<% else %>
<%= text_field(:user, :subdivision) %>
<% end %>
</div>
_form.html.erb
<%= form_for(@user) do |f| %>
<div class="field">
<%= f.label :country %><br />
<%= f.country_select :country, priority: %w(US CA), prompt: 'Please select a country' %>
</div>
<div class="field">
<%= f.label :subdivision, "State/Province" %><br />
<%= render 'subregion_select', locals: {parent_region: f.object.country, :sub_region => f.object.subdivision } %>
</div>
<div class="field">
<%= f.label :city %><br>
<%= f.text_field :city %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
users.js.coffee
$(document).on 'ready page:load', ->
$('select#user_country').change (event) ->
select_wrapper = $('#order_state_code_wrapper')
$('select', select_wrapper).attr('disabled', true)
country = $(this).val()
url = "/subregion_options?parent_region=#{country}"
select_wrapper.load(url)
users_controller.rb
def subregion_options
render partial: 'subregion_select', locals: { parent_region: params[:parent_region], sub_region: params[:sub_region] }
end
Any insight would help, thank you!
Aucun commentaire:
Enregistrer un commentaire