当我选择一个国家/地区时,如何在搜索中创建一个 ajax,它会显示我在所选国家/地区拥有的所有州
我正在尝试创建一个显示,当我选择国家时自动显示所有选择国家的州
我的桌子
Countries
|id| |country_name|
1 'USA'
2 'Peru'
States
|id| |state_name|
1 Alabama
2 Machupicchu
Country_States
|id| |country_id| |state_id|
1 1 1
2 2 2
我的控制器
class Country_StatesController < ApplicationController
def conditional
@countries = Country.find(:all)
@states= State.find(:all)
@selected_country = Country.find_by_id(params[:countries]) if params[:countries].to_i
@selected_state = State.find_by_id(params[:states]) if params[:states].to_i
@search= CountryState.find(:all,:conditions=> ['state_id','country_id' ],params[:states],params[:countries] )
end
end
我的观点
<% form_tag :controller=>"country_States",:action=>"conditional" do %>
<%= select_tag "countries", options_for_select(@countries.collect {|t| [t.state_name.to_s ,t.id]}) %>
<%= select_tag "states", options_for_select(@states.collect {|t| [t.state_name.to_s ,t.id]}, params[:search].to_i ) %>
<%= submit_tag "Search", :name => nil %>
<% end %>
我发现了这样的东西
<%= collection_select :selection, :level, User::LEVELS, :to_s, :to_s, {},
{:onchange => remote_function(
:url => {:action => "updatelevel", :controller => "user", :id=> user.id},
:with => "'level_id='+this.value"
)
}
%>
我会很感激帮助。