How can i create an ajax in my search when i select a country it would show me all states that i have in the country selected
I'm trying to create a display when i select country automatically show all the states that has the country selected
My tables
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
My controller
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
My view
<% 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 %>
I found something like this
<%= 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"
)
}
%>
I will appreciate help.