0
我已經將我的級聯collection_select表單按照我想要的方式工作,但現在我想知道如何按字母順序排序子字段的更新結果。在我的控制器Rails - 如何對層疊集合的結果進行排序選擇表格
我的自定義操作:
def update_areas
city_id = (params[:city_id].nil? || params[:city_id].empty?) ? 0 : params[:city_id].to_i
# updates artists and songs based on genre selected
if city_id == 0
@areas = []
@neighborhoods = []
else
city = City.find(params[:city_id])
# map to name and id for use in our options_for_select
@areas = city.areas.map{|a| [a.name, a.id]}
@neighborhoods = Neighborhood.where(:area_id => city.areas.each{|a| a.id}).map{|s| [s.name, s.id]}
end
@areas.insert(0, "Select an Area")
@neighborhoods.insert(0, "Select a neighborhood")
end
def update_neighborhoods
# updates songs based on artist selected
area = Area.find(params[:area_id])
@neighborhoods = area.neighborhoods.map{|s| [s.name, s.id]}.insert(0, "Select a neighborhood")
end
表單代碼:
<div id="city">
<p>City:</p>
<%= f.collection_select(:city_id, City.order('name ASC'), :id, :name, {:prompt => "Select a City"}, {:id => 'cities_select'}) %>
<br />
</div>
<div id="area">
<p>City area:</p>
<%= f.collection_select(:area_id, [], :id, :name, {:prompt => "Select an Area"}, {:id => 'areas_select'}) %>
<br />
</div>
<div id="neighborhood">
<p>Neighborhood:</p>
<%= f.collection_select(:neighborhood_id, [], :id, :name, {:prompt => "Select a Neighborhood"}, {:id => 'neighborhoods_select'}) %>
<br />
</div>
我知道我可以做的形式代碼類似Area.order( '名ASC'),但我想讓它保持空白,直到城市被選中。
真棒!謝謝! – Zephyr4434 2013-05-13 03:53:00
不客氣。請將答案標記爲正確的答案。謝謝 – 2013-05-13 04:12:12