如何列出獨特的國家,其次是相關城市?下面是我的產品表:Rails得到獨一無二的國家與城市
name country city
p1 US New York
p2 US Boston
p3 US Chicago
k1 UK London
k2 UK Liverpool
控制器:
@countries = Product.joins(:user).distinct.where("country is not null and country <> ''").where(:users => {:merchant_status => 1}).pluck(:country)
@cities = Product.joins(:user).distinct.where("city is not null and city <> ''").where(:users => {:merchant_status => 1}).pluck(:city)
@countries.map! {|country| country.split.map(&:capitalize).join(' ')}
@search_location_country = @countries
在我看來:
<ul id="color-dropdown-menu" class="dropdown-menu dropdown-menu-right" role="menu">
<% @search_location_country.each do |country| %>
<li class="input"><a href="#"><%= country %></a></li>
<% end %>
</ul>
我如何排序的最終結果下拉這樣的:
US
- New York
- Boston
- Chicago
UK
- London
- Liverpool
謝謝!
編輯
要顯示這樣的事情:
是什麼'@城市'爲? – sschmeck
@sschmeck城市我用於搜索沒有下拉。所有的國家和城市搜索工作都很好,但我需要理清它的顯示方式。國家其次是城市名單 – d3bug3r
你正在尋找select2元素或類似的我tink – Guru