0

carmen-railsRails プロジェクトで gemを使用したいのですが、 githubのドキュメントでは使用方法がよくわかりません。たとえば、ユーザーに国と州を持たせたい場合:

まず、ユーザー モデル (国と州) に2 つの列を追加する必要があります。

2番目:選択した国と州をユーザーフォームに追加します:

<%= 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 %>

次に、私のパーシャルは次のようになります。

<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 %>

私は正しい ?

フォームが送信されたときに国と州を検証する方法は?

最後に、選択フォームで国と州の言語を変更する方法(たとえばフランス語に)?

4

1 に答える 1

1

はい、はい。

彼らのデモアプリを見てみましょう: https://github.com/jim/carmen-demo-app/

ルートを追加して状態のパーシャルを埋める必要があります。次に、適切なコントローラー メソッドを追加してパーシャルをレンダリングする必要があります。最初に部分ルートを定義し、次にユーザー コントローラー ルートを定義することを忘れないでください。

例 (routes.rb)

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

例 (users_controller.rb)

def subregion_options
  render partial: 'subregion_select'
end

国の選択で変更イベントをキャッチする JS を追加し、国コードを使用してユーザー コントローラー メソッドを呼び出して州を部分的にレンダリングする必要があります。

例 (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)

通常どおり、ユーザーのモデル クラスの検証を処理できます。サンプル アプリには、ロケールの変更に関する情報が含まれています。

于 2013-11-08T23:39:14.740 に答える