Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Javascript
- function update_states_div(country_id) {
- jQuery.ajax({
- url: "/update_states",
- type: "GET",
- data: {"country_id" : country_id},
- dataType: "html"
- success: function(data) {
- jQuery("#statesDiv").html(data);
- }
- });
- }
- // Controller
- def new
- @User = User.new
- @countries = Country.all
- @states = State.al
- end
- def edit
- @User = User.find(params[:id])
- @countries = Country.all
- @states = State.all
- end
- def update_states
- @states = State.where(country_id => params[:country_id])
- render :partial => "states", :object => @states
- end
- // View
- <%= select_tag "country_id", options_from_collection_for_select(@countries, "id", "name"), :prompt => "Select Your Country", :onchange => "update_states_div(this.value)" %>
- <div id="statesDiv">
- <%= render :partial => 'states', :object => @states %>
- </div>
- // Partial: _state.html.erb
- <%= select_tag "state_id", options_from_collection_for_select(states, "id", "name"), :prompt => "Select State" %>
- // Also add a route for /update_states in your routes.rb
- match "/update_states" => "users#update_states"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement