0

私はこのrailscastに従い、国と都市を動的選択メニューとして表示して、新しいプロファイルを追加する際に国と都市を選択することに成功しました. app/views/searchに index アクションとビュー " index.html.erb " を持つコントローラー " search_controller.rb " を作成しました。今、私は app/views/search/index.html.erb に検索フォームを作成したいと思います

  1. プロファイルの件名で検索するオートコンプリート テキスト フィールド (この表では約 400 件の件名)
  2. プロファイルの国と都市で検索する国と都市の動的選択メニュー。

アプリ/モデル/プロファイル.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()
4

1 に答える 1

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

app/assets/javascripts/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                                                                   
});
于 2013-10-09T20:53:06.243 に答える