国と都道府県を別々の選択フィールドに表示したいと思います。
ただし、都道府県は、国名とテーブル内のすべての都道府県の両方を一緒に表示します。
都道府県のレコードを、前のフィールド選択で選択した国に属する都道府県のみに制限するにはどうすればよいですか?
私のモデルには、次のようなモデルがあります
- ユーザー (このモデルには 1 つの UserProfile があります)
- UserProfile (このモデルは User に属し、1 つの Country と 1 つの都道府県を持ちます)
- Country (このモデルは UserProfile に属し、都道府県が多数あります)
- 都道府県 (このモデルは Country と UserProfile に属します)
_form.html.erb
<%= f.fields_for :user_profile do |profile_form| %>
<%= profile_form.label :country_id %><br />
<%= profile_form.collection_select :country_id, Country.order(:name_en), :id, :name_en, include_blank: true %>
<%= profile_form.label :prefecture_id, "State or Province" %><br />
<%= profile_form.grouped_collection_select :prefecture_id,Country.order(:name_en),:prefectures, :name_en, :id, :name,include_blank: true %>
<% end %>
models/country.rb
has_many :都道府県
models/prefectures.rb
所属先:国