Country State City using AJAX

Hi Aziz…

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/’;
$(“#client_country_id”).change(function() {
var country_id = $(this).val();
$.ajax({
type: “GET”,
data: {country: country_id},
url: country_states_path+country_id,
success: function(data) {
$(“#client_country_id”).html(data)
}
});
});
});

On Thu, May 3, 2012 at 5:36 PM, Aziz B.
[email protected]wrote:

def index
def show
def new
end
format.json { render json: @state, status: :created, location:
@state = State.find(“country_id = :c_id”,{:c_id => params[:country]})

end
  format.json { head :ok }

resources :countries

resource.

states controller, and in application.js i have modified the path like
this

Hey AJ

Hey Aziz,
[email protected]> wrote:

    var state_id = $(this).val();

Incase you are not using nested resources, you still need to
url: country_states_path,
[email protected]> wrote:

type: “GET”,

it… do i include the above div in a prtial and what to
[email protected]>

not work?
[email protected].

[email protected].

[email protected].

[email protected].

[email protected].

To unsubscribe from this group, send email to

For more options, visit this group at
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks & Regards,
Akshar Jamgaonkar

Hello All,

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


          Iam trying to achiev  a functionlity of state drop down

been populated on click of country…i have achieved almost 90%
of this…iam getting appropriate responce in my firebug. responce
is like this when i click india…

Goa

my application .js looks like this

$(document).ready(function() {
var country_states_path = ‘/states/countries_state/’;
$(“#client_country_id”).change(function() {
var country_id = $(this).val();
$.ajax({
type: “GET”,
data: {country: country_id},
url: country_states_path+country_id,
success: function(data) {
$(“#client_state_id”).html(data)
}
});
});
});

the controller state has an action like this

def countries_state
@state = State.where(“country_id = :c_id”,{:c_id =>
params[:country_id]})
respond_to do |format|
format.js
end
end

and template for this action is like

<%= select(“states”, “state_id”, @state.collect{|s| [s.name,s.id]}) %>

However i am getting the response in firebug but the html on page is
not updated. Iam trying to find but iam stuck as iam a bit new to all
this.

Please suggest as to what might be wrong. thanks a lot

Thanks & Regards,
Akshar


On May 3, 5:52pm, akshar jamgaonkar [email protected]

On 3 May 2012, at 14:06, AJ [email protected] wrote:

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 W.

Hi all,

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

Maharashtra.

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

On Thu, May 3, 2012 at 5:58 PM, akshar jamgaonkar <
[email protected]> wrote:

the console in firbug when i see i get the states for a specific
type: “GET”,

GET /states.json

GET /states/1.json

GET /states/new.json

@state = State.find(params[:id])

successfully created.’ }
def countries_state
@state = State.find(params[:id])
end
format.html { redirect_to states_url }


Make sure ur using the currect URL for the new action.

resources :discounts

“states#countries_state”

On Thu, May 3, 2012 at 5:03 PM, Aziz B. <

On Thu, May 3, 2012 at 5:01 PM, akshar jamgaonkar <

$(document).ready(function() {
});
$.ajax({

$(document).ready(function() {

Thanks & Regards,

You received this message because you are subscribed to the

For more options, visit this group at
You received this message because you are subscribed to the

For more options, visit this group at
You received this message because you are subscribed to the

For more options, visit this group at
You received this message because you are subscribed to the

For more options, visit this group at
You received this message because you are subscribed to the Google

For more options, visit this group at
You received this message because you are subscribed to the Google

For more options, visit this group at
You received this message because you are subscribed to the Google


Thanks & Regards,
Akshar Jamgaonkar


Thanks & Regards,
Akshar Jamgaonkar

On 3 May 2012, at 14:24, akshar jamgaonkar [email protected]
wrote:

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.

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

On Thu, May 3, 2012 at 6:46 PM, Jeremy W. [email protected]
wrote:

run $(“#client_state_id”) in firebug?

is like this when i click india…
var country_id = $(this).val();

end
this.

@state = State.where(“country_id = :c_id”,{:c_id =>

counytry…The responce is like “[["Goa", 2]]” …
type: “GET”,
[email protected]>wrote:

On Thu, May 3, 2012 at 5:23 PM, akshar jamgaonkar <


  format.json { render json: @states }
  format.json { render json: @state }
end
@state = State.new(params[:state])

:unprocessable_entity }
end
successfully updated.’ }

DELETE /states/1.json

On Thu, May 3, 2012 at 5:18 PM, akshar jamgaonkar <

resources :roles

On Thu, May 3, 2012 at 5:12 PM, akshar jamgaonkar <

Did you add a route for this new action?
however

Hey AJ
[email protected]> wrote:

AJ

Groups “Ruby on Rails: Talk” group.
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks & Regards,
Akshar Jamgaonkar

On 3 May 2012 14:38, akshar jamgaonkar [email protected]
wrote:

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:

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 (
    Rails Routing from the Outside In — Ruby on Rails Guides)
  • 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

On Thu, May 3, 2012 at 7:31 PM, Jeremy W. [email protected]
wrote:

request and render the states back as a JSON array. Let the browser and

Yes i have a Select box with this id = “client_state_id” … when i run

On 3 May 2012, at 14:06, AJ [email protected] wrote:

value=“2”>Goa
data: {country: country_id},
the controller state has an action like this

is the

respond_to do |format|
Thanks
however

        type: "GET",

[email protected]>wrote:

On Thu, May 3, 2012 at 5:23 PM, akshar jamgaonkar <


  format.json { render json: @states }
  format.json { render json: @state }
end
@state = State.new(params[:state])

:unprocessable_entity }
end
successfully updated.’ }

DELETE /states/1.json

On Thu, May 3, 2012 at 5:18 PM, akshar jamgaonkar <

resources :roles

On Thu, May 3, 2012 at 5:12 PM, akshar jamgaonkar <

Did you add a route for this new action?
however

Hey AJ
[email protected]> wrote:

Thanks,
structure.

To unsubscribe from this group, send email to
For more options, visit this group at
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks & Regards,
Akshar Jamgaonkar

Hey Akshar

Good to see that you got it working.

On Fri, May 4, 2012 at 2:51 PM, akshar jamgaonkar <
[email protected]> wrote:

On Thu, May 3, 2012 at 7:02 PM, Jeremy W. [email protected]wrote:

my application .js looks like this
$(“#client_state_id”).html(data)
@state = State.where(“country_id = :c_id”,{:c_id =>
<%= select(“states”, “state_id”, @state.collect{|s| [s.name,s.id]})


<option

end
[email protected]> wrote:

        success: function(data) {

a nested resource. You can read about that here<

Iam sorry i didnt get you… i have created a new
class StatesController < ApplicationController

GET /states/1/edit

  if @state.save

end

PUT /states/1

    format.html { render action: "edit" }
@state.destroy

Yea it looks like this…
resources :level_of_contacts

Yes i have added…it looks like this

[email protected]> wrote:
… Iam
would need

On Thu, May 3, 2012 at 4:46 PM, Aziz B. <

$(document).ready(function() {
To post to this group, send email to
You received this message because you are subscribed to the Google

For more options, visit this group at

To post to this group, send email to [email protected].
To unsubscribe from this group, send email to


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

  • Aziz M. Bookwala

Website http://azizmb.in | Twitter https://twitter.com/azizbookwala
|
Github http://github.com/azizmb

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…

On Thu, May 3, 2012 at 7:02 PM, Jeremy W. [email protected]
wrote:

what might be wrong
Iam trying to achiev a functionlity of state drop down
$(document).ready(function() {
});
respond_to do |format|

On May 3, 5:52 pm, akshar jamgaonkar [email protected]

but the html on the page is not updating…any reson why this

dynamically but when i make it format.html, the state drop down

Iam getting whatevr reponce iam nneding,....... Thansk a
$("#client_country_id").change(function() {

});

using in

respond_to do |format|

POST /states

  else

respond_to do |format|
respond_to do |format|
end
end

resources :clients

country

On Thu, May 3, 2012 at 5:10 PM, Aziz B. <
modified the path like

On Thu, May 3, 2012 at 5:03 PM, Aziz B. <
quite easily.

country and how do i render the state drop down in

In your ajax call, you need to pass the id of the
read more

To post to this group, send email to [email protected].
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks & Regards,
Akshar Jamgaonkar

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