`
sun201200204
  • 浏览: 295684 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

多表联动

阅读更多
test.rhtml的代码:
<%= javascript_include_tag :defaults %>
<%= select(:city, :province_id, @provinces, {},
{"onchange" => remote_function(
  :with => "'province_id='+value",
  :update => 'city_select',
  :url => { :action => :select_cities_with_ajax })})
%>

<div id='city_select'>
<%= select( :county, :city_id, @cities, options = {},
html_options = { "onchange" => remote_function(
  :with => "'city_id='+value",
  :update => 'county_select',
  :url => { :action => :select_counties_with_ajax })})
%>
</div>
<div id='county_select'><%= select(:sth, :county_id, @counties) %></div>

_select_city.rhtml代码:
<%= select(:county, :city_id, @cities, {},
{"onchange" => remote_function(
  :with => "'city_id='+value",
  :update => 'county_select',
  :url => { :action => :select_county_with_ajax } )} )
%>

_select_county.rhtml代码:
<%= select(:sth, :county_id, @sights) %>

controller代码:
def select_city_with_ajax
@cities = Province.find(params[:province_id]).cities.map{|u| [u.name,u.id]}
render :partial => "select_city"
end

def select_county_with_ajax
@counties = City.find(params[:city_id]).counties.map{|u| [u.name,u.id]}
render :partial => "select_county"
end
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics