1,在layouts中
添加 <%= javascript_include_tag 'prototype' %>
2,在views中
添加
<% members = City.find_by_sql("select * from cities where cityid like '%0000'").collect { |c| [c.name,c.cityid] }%>
<%= select '', :abc,members,{:include_blank => '--请选择--'},
{ "onchange" => remote_function(:with => "'abc='+value",:update => 'next_select', :url => { :action => :select_with_ajax } ) }
%>
<span id='next_select'></span>
注意:select中有四个参数,第一个参数为空,第二个参数设置了一个名为abc的变量,以便把选择到的省的ID传到下一级的select中。
3,在controller中
添加
def select_with_ajax
@city = City.find_by_sql("select * from cities where (cityid mod 100 =0 )and (cityid div 10000 = #{params[:abc]} div 10000 ) and (cityid != #{params[:abc]})").collect { |city| [city.name, city.cityid] }
render(:layout => false)
end
4,在views中
新建select_with_ajax.erb文件
然后添加 <%= select( :address,:city_id, @city) %>
总:二级联动菜单这样写,目的是把第二个select中选择的市的ID传到表的相应字段中。