2013-10-09 72 views
0

我跟着这个railscast成功地显示国家和城市作为动态选择菜单来选择添加新配置文件的国家和城市。我在app/views/search中创建了一个带有索引操作的控制器“search_controller.rb”,并创建了一个控制器“index.html.erb”。现在,我想创建的应用程序/视图/搜索/ index.html.erb搜索表单具有如何在搜索表单中添加动态选择菜单

  1. 自动填充文本字段通过配置文件的受试者(几乎400名受试者在此表)
  2. 国家和城市进行搜索动态选择菜单,可通过个人资料的国家和城市进行搜索

应用程序/模型/ 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() 
+0

对于那些正在研究并使用[Railscast](http://railscasts.com/episodes/88-dynamic-select-menus-revised)作为帮助的人士,我们提供了一个修订版本。 – samuelkobe

回答

0

这是我与sunspot-solr搜索和twitter-typeahead-rails为自动完成的解决方案。在模型中添加可搜索的块后,我使用了以下代码用于我的应用程序。我通过twitter typeahead将本人的主题列表加载到本地存储中,因为它不会更改,但如果需要,可以使用json。

应用程序/控制器/ search_controller.rb

def index 
    @search = Profile.search do 
    fulltext params[:search] 
    with(:city_id, params[:city]) if params[:city].present? 
    end 
    @profiles = @search.results 
end 

应用/视图/搜索/ index.html.erb

<%= text_field_tag :search, params[:search], :class => 'typeahead' %> 
<div class="field"> 
    <%= select_tag :country, options_from_collection_for_select(Country.all, :id, :name, params[:country]), include_blank: true %> 
</div> 
<div class="field"> 
    <%= select_tag :city, option_groups_from_collection_for_select(Country.order(:name), :cities, :name, :id, :name, params[:city]), include_blank: true %> 
</div> 
<%= submit_tag "search", :name => nil, :class => 'btn btn-default' %> 

应用程序/资产/ Javascript角/ profiles.js .coffee

$('#city').parent().hide() 
cities = $('#city').html() 
$('#country').change -> 
    country = $('#country :selected').text() 
    escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1') 
    options = $(cities).filter("optgroup[label='#{escaped_country}']").html() 
    if options 
    $('#city').html(options) 
    $('#city').parent().show() 
    else 
    $('#city').empty() 
    $('#city').parent().hide() 

$('.typeahead').typeahead({         
    local: ["3D Design", "Architecture", "Chemical engineering", ...] 
    limit: 10                 
});