HI , I have a state and country drop down in my client form and i
want that state to be populated when a country is selected, How ever
iam struggling on it... i dont want to implement plain JavaScript but
AJAX for this, please let me know any resources or the rite
direction..
now i want to pass the country id and ftecth state depending on
it..... do i include the above div in a prtial and what to write in
the state controller.....
In your ajax call, you need to pass the id of the selected state if you are using a nested resource url structure.
Your js would looks something like this:
$(document).ready(function() {
var country_states_path = '/countries/:id/states;
Incase you are not using nested resources, you still need to pass the state_id that was selected currently. Using the below js, in your controller, the state id would be available as params[:state]
$(document).ready(function() {
var country_states_path = ‘/states/’;
$(“#client_country_id”).change(function() {
var country_id = $(this).val();
$.ajax({
type: “GET”,
really thanks for you repl…so do i need to write a controller action that would return me the states depending on the country and how do i render the state drop down in Cilent Form…Thanks…
Yes, to serve any request made by your client, you would need something on the server to handle the request.
As for rendering the dropdown, this is a pretty simple thing to do with jQuery. Look around, you should find something quite easily.
That is fine, though you could make it more ‘RESTful’ by adding it as a nested resource. You can read about that here. The RailsCast is a little old, but you should get the idea.
With respect to your current problem, what is the url you are using in your js?
Iam getting whatevr reponce iam nneding,… Thansk a lot…in the console in firbug when i see i get the states for a specific counytry…The responce is like “[["Goa", 2]]” … however whatever processing i do in application.js in not taking effect,
My application.js is
$(document).ready(function() {
var country_states_path = ‘/states/countries_state/’;