Country State City using AJAX

Also in the def countries_state

@state = State.where("country_id = :c_id",{:c_id => params[:country_id]})

respond_to do |format|

format.html

end

end

previously the it was format.js when nothing on the page was changing dynamically but when i make it format.html, the state drop down changes but i get all the unnecessary thing like the the entire html page.

Thanks

Hi all,

I have succesfully got a responce like this as to what i want , this is the ouptut in my fireofx console.

.

but the html on the page is not updating…any reson why this could be …thanks a lot…


Hello All,

ill just summarize everything in this message..kindly suggest as to what might be wrong

Hello All,

ill just summarize everything in this message..kindly suggest as to what might be wrong

Do you have a div with the id of "client_state_id"? What do you get if you run $("#client_state_id") in firebug?

Jeremy Walker

Yes i have a Select box with this id = “client_state_id” … when i run $(“#client_state_id”) the select drop down control gets highlighted. Thanks

Yes i have a Select box with this id = “client_state_id” … when i run $(“#client_state_id”) the select drop down control gets highlighted. Thanks

Try using replaceWith() instead of html() in your success function.

HEy cool… Thanks a lot…it worked however with state drop down i also iam getting a lot of other un necessary html… The html is what i have written in application.html.erb for templating purpose… Is this the wrong way to write static html code in application.html.erb… Thanks a lot…

HEy cool… Thanks a lot…it worked however with state drop down i also iam getting a lot of other un necessary html… The html is what i have written in application.html.erb for templating purpose… Is this the wrong way to write static html code in application.html.erb… Thanks a lot…

You need to tell your view not render the layout. You should read the Rails guide to rendering and layouts. I’ll let you figure it out from there:

http://guides.rubyonrails.org/layouts_and_rendering.html

Just for your reference, I feel that a better way of achieving what you’re doing would be:

  • Have nested routes for countries and states and call the url at /countries/1/states.json (http://guides.rubyonrails.org/routing.html#nested-resources)

  • Have the index method of the StatesController respond to the .json request and render the states back as a JSON array. Let the browser and create the tags using Javascript.

IMHO that way is quicker, slicker and more maintainable.

Also, in your current form, you could replace your $.ajax method with:

$.get(country_states_path+country_id, function(data) {

$(“#client_state_id”).replaceWith(data);

}

});

Hope that’s helpful.

Hey Guys… All of You… Thanks alot… i have achieved it…i was struglling on it from 2 3 days…and without all of your help i wouldnt have successeded… RUBY ON RAILS ROCK…thanks

Hey Akshar

Good to see that you got it working.

Yea Aziz…thanks a lot

I want Country State City using AJAX in rails 4 code.

The first problem here is that you have asked a very general question which could mean a number of different things. Do you mean you want to get these from your own website or are you looking for another server somewhere that you can get them from? If from your own website do you already have the data in the database?

Please try to ask more specific questions in future. Otherwise we spend a lot of time just trying to find out what you are trying to do.

Colin