我跟着這個railscast成功地顯示國家和城市作爲動態選擇菜單來選擇添加新配置文件的國家和城市。我在app/views/search中創建了一個帶有索引操作的控制器「search_controller.rb」,並創建了一個控制器「index.html.erb」。現在,我想創建的應用程序/視圖/搜索/ index.html.erb搜索表單具有如何在搜索表單中添加動態選擇菜單
- 自動填充文本字段通過配置文件的受試者(幾乎400名受試者在此表)
- 國家和城市進行搜索動態選擇菜單,可通過個人資料的國家和城市進行搜索
應用程序/模型/ profile.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/a ssets/Java腳本/ 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()
對於那些正在研究並使用[Railscast](http://railscasts.com/episodes/88-dynamic-select-menus-revised)作爲幫助的人士,我們提供了一個修訂版本。 – samuelkobe