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