私はこのrailscastに従い、国と都市を動的選択メニューとして表示して、新しいプロファイルを追加する際に国と都市を選択することに成功しました. app/views/searchに index アクションとビュー " index.html.erb " を持つコントローラー " search_controller.rb " を作成しました。今、私は app/views/search/index.html.erb に検索フォームを作成したいと思います
- プロファイルの件名で検索するオートコンプリート テキスト フィールド (この表では約 400 件の件名)
- プロファイルの国と都市で検索する国と都市の動的選択メニュー。
アプリ/モデル/プロファイル.rb
has_many :categorizations
has_many :subjects, through: :categorizations
belongs_to :country
belongs_to :city
アプリ/モデル/subject.rb
has_many :categorizations
has_many :profiles, through: :categorizations
アプリ/モデル/categorization.rb
belongs_to :profile
belongs_to :subject
アプリ/モデル/country.rb
has_many :cities
has_many :profiles
アプリ/モデル/city.rb
belongs_to :country
has_many :profiles
アプリ/ビュー/プロファイル/_form.html.erb
<div class="field">
<%= f.label :country_id %><br />
<%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: true %>
</div>
app/assets/javascripts/profiles.js.coffee
$('#profile_city_id').parent().hide()
cities = $('#profile_city_id').html()
$('#profile_country_id').change ->
country = $('#profile_country_id :selected').text()
escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
options = $(cities).filter("optgroup[label='#{escaped_country}']").html()
if options
$('#profile_city_id').html(options)
$('#profile_city_id').parent().show()
else
$('#profile_city_id').empty()
$('#profile_city_id').parent().hide()