Question

I want to use carmen-rails gem in my rails project, but in documentation at github i can't understand well how to use it , so i think : if for example i want my users have a country and state :

first : i should add 2 columns in my user model (country and state), Right ?

second : add select country and state to my user form :

<%= simple_form_for @user do |f| %>
<div class="field">
  <%= f.label :country_code %><br />
  <%= f.country_select :country_code, priority: %w(US CA), prompt: 'Please select a country' %>
</div>

<div class="field">
  <%= f.label :state_code %><br />
  <%= render partial: 'subregion_select', locals: {parent_region: f.object.country_code} %>
</div>
<% end %>

then my partial should be look like :

<div id="order_state_code_wrapper">
<% parent_region ||= params[:parent_region] %>
<% country = Carmen::Country.coded(parent_region) %>

<% if country.nil? %>
  <em>Please select a country above</em>
<% elsif country.subregions? %>
  <%= subregion_select(:order, :state_code, parent_region) %>
<% else %>
  <%= text_field(:order, :state_code) %>
<% end %>

I'm right ?

then how to validate country and state when the form is submitted ?

finally how to change language of countries and states in the select form (to french for example ) ?

No correct solution

OTHER TIPS

Yes and yes.

Take a look at their demo app: https://github.com/jim/carmen-demo-app/

You need to add the route to fill the states partial, then add the appropiate controller method to render the partial. Don't forget to define the partial route first and then users controller routes.

e.g. (routes.rb)

get '/users/subregion_options' => 'users#subregion_options'
resources :users

e.g. (users_controller.rb)

def subregion_options
  render partial: 'subregion_select'
end

You need to add some JS to catch change event on countries select and then with the country code call the users controller method to render the states partial.

e.g. (users.js.coffee)

$ ->
  $('select#user_country_code').change (event) ->
    select_wrapper = $('#user_state_code_wrapper')

    $('select', select_wrapper).attr('disabled', true)

    country_code = $(this).val()

    url = "/users/subregion_options?parent_region=#{country_code}"
    select_wrapper.load(url)

You can handle validation on the user's model class as usual. The sample app includes information on changing locales.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top