Implementing countries and states drop down

Hi all,

I am implementing the functionality of cascading drop downs of countries
and states. I want this to be done with Ajax and ruby so that the page
dont get refreshed on selecting the country in first drop down.

Currently I have implemented it with java script and ruby. But the
problem is the page get refreshed when i select the country from
countries drop down. Can some one help me out in implementing this
functionality.

Any help would be greatly appreciated.

-Regards
Ruchita S…

Ruchita S. wrote:

Hi all,

I am implementing the functionality of cascading drop downs of countries
and states. I want this to be done with Ajax and ruby so that the page
dont get refreshed on selecting the country in first drop down.

Currently I have implemented it with java script and ruby. But the
problem is the page get refreshed when i select the country from
countries drop down. Can some one help me out in implementing this
functionality.

Any help would be greatly appreciated.

-Regards
Ruchita S…
Hi Ruchita,

In Ajax the main advantage is that we can refresh the page in parts ie,
buy defining divisions in view file as

and updating only this div as :update => "some name"

But at this juncture what I feel is that it is not due to abscence of
Ajax but it is due to your code logic that the page is refreshing while
selecting country only. Any way may be I can help you if you can send me
some code especially that part where you implement drop down list

All the Best

Abhi M. wrote:

Ruchita S. wrote:

Hi all,

I am implementing the functionality of cascading drop downs of countries
and states. I want this to be done with Ajax and ruby so that the page
dont get refreshed on selecting the country in first drop down.

Currently I have implemented it with java script and ruby. But the
problem is the page get refreshed when i select the country from
countries drop down. Can some one help me out in implementing this
functionality.

Any help would be greatly appreciated.

-Regards
Ruchita S…
Hi Ruchita,

In Ajax the main advantage is that we can refresh the page in parts ie,
buy defining divisions in view file as

and updating only this div as :update => "some name"

But at this juncture what I feel is that it is not due to abscence of
Ajax but it is due to your code logic that the page is refreshing while
selecting country only. Any way may be I can help you if you can send me
some code especially that part where you implement drop down list

All the Best

Hi Abhi M.,

The part of the code where i am implementing it as follows. Can you plz
tell me where is the problem.

First Name Last Name Country
<% @countries.each do |country| %> > <%= country.country_name %> <% end %>

<% @rubyVar=request.parameters[“categoryid”] %>
<input type =“text” id=“categorytxt” name=“textbox1” size=“30” value=
<%= @rubyVar %> > State <% @states.each do |state| %> <% if (state.country_id == (@rubyVar.to_i)) %> > <%= state.state_name %> <% end %> <% end %> City
Zip Code

Thanks,
Ruchita S.

Hi Ruchita

In your code you are telling like

ie whenever there is a change in country dropdown selection list control
should go to function goToSub() which inturn will load the other part
rt?. So that is why it is getting refreshed

I will check some solution and get back to you

Thanks
Abhimanyu

Abhi M. wrote:

Hi Ruchita

In your code you are telling like

ie whenever there is a change in country dropdown selection list control
should go to function goToSub() which inturn will load the other part
rt?. So that is why it is getting refreshed

I will check some solution and get back to you

Thanks
Abhimanyu

Yeah… In function goToSub(); I am fetching the value of country id.
window.location="…/user/new_client?categoryid=" +
category2.options[category2.selectedIndex].value;

Then i am getting this id by using this line of code:

<% @rubyVar=request.parameters[“categoryid”] %>

In the states drop down i am comparing:

if (state.country_id == (@rubyVar.to_i))
then corresponding to this, states are displayed.

The problem is whenever I select the country, the fields which are above
the country are lost perhaps due to refreshing of page.

Thanks,
Ruchita S.

Abhi M. wrote:

Abhi M. wrote:

Hi Ruchita

In your code you are telling like

ie whenever there is a change in country dropdown selection list control
should go to function goToSub() which inturn will load the other part
rt?. So that is why it is getting refreshed

I will check some solution and get back to you

Thanks
Abhimanyu

Hi Ruchita

Sorry I have to reach some place. so I will post you tommorrow only.
Also try to use Collection_select instead of “option” to show drop down
list so that you can include many options. check some tutorials on
collection_select if you dont know that or else I will tell you tomorrow

Thanks

Abhimanyu

Hi

Thanks a lot. Not an issue but please do help me out.

-Ruchita

Hi

<%= collection_select("country","name",@countries,"id","name",{:onchange => "javascript:selectedData(this.options[selectedIndex].value)"}) %>

Abhi M. wrote:

Hi Ruchita

In your code you are telling like

ie whenever there is a change in country dropdown selection list control
should go to function goToSub() which inturn will load the other part
rt?. So that is why it is getting refreshed

I will check some solution and get back to you

Thanks
Abhimanyu

Hi Ruchita

Sorry I have to reach some place. so I will post you tommorrow only.
Also try to use Collection_select instead of “option” to show drop down
list so that you can include many options. check some tutorials on
collection_select if you dont know that or else I will tell you tomorrow

Thanks

Abhimanyu

Abhi M. wrote:

Hi

<%= collection_select("country","name",@countries,"id","name",{:onchange => "javascript:selectedData(this.options[selectedIndex].value)"}) %>

function selectedData(selectedvalue)
{
var codeid = selectedvalue;
<%= remote_function(:update => ‘show’, :url =>
showdescription_url,:with => “‘value=’+encodeURIComponent(codeid)”)%>
}

Abhi M. wrote:

Abhi M. wrote:

Hi

<%= collection_select("country","name",@countries,"id","name",{:onchange => "javascript:selectedData(this.options[selectedIndex].value)"}) %>

function selectedData(selectedvalue)
{
var stateid = selectedvalue;
<%= remote_function(:update => ‘show’, :url =>
showstate_url,:with => “‘value=’+encodeURIComponent(stateid)”)%>
}

you have to use roots.rb
then shoud have a div called show in the view file where you specify the
drop down list of countries

<%=render:partial => states> <\div>

Abhi M. wrote:

Abhi M. wrote:

Abhi M. wrote:

Hi

<%= collection_select("country","name",@countries,"id","name",{:onchange => "javascript:selectedData(this.options[selectedIndex].value)"}) %>

function selectedData(selectedvalue)
{
var stateid = selectedvalue;
<%= remote_function(:update => ‘show’, :url =>
showstate_url,:with => “‘value=’+encodeURIComponent(stateid)”)%>
}

you have to use roots.rb
then shoud have a div called show in the view file where you specify the
drop down list of countries

<%=render:partial => states> <\div>

Hi,

Can you gmmme full code. Am not able to implement it in my code.

Hi,
I have given only some idea to you. Its not part of any code . You can
use collection select for drop down list
and if any country is selected then javascript function function
selectedData(selectedvalue) is called. In that function itself it will
call other action

function selectedData(selectedvalue)
{
var stateid = selectedvalue;
<%= remote_function(:update => ‘show’, :action =>
action_name,:with => “‘value=’+encodeURIComponent(stateid)”)%>
}

write the state drop down list in a partial file and render that file in
the above action you call

and also define a div called show in the view file of country drop down
and render the partial file here also.

Please try this

Abhi M. wrote:

Hi,
I have given only some idea to you. Its not part of any code . You can
use collection select for drop down list
and if any country is selected then javascript function function
selectedData(selectedvalue) is called. In that function itself it will
call other action

function selectedData(selectedvalue)
{
var stateid = selectedvalue;
<%= remote_function(:update => ‘show’, :action =>
action_name,:with => “‘value=’+encodeURIComponent(stateid)”)%>
}

write the state drop down list in a partial file and render that file in
the above action you call

and also define a div called show in the view file of country drop down
and render the partial file here also.

Please try this

Okie… let me try to implement this. Thanks for your help. I’ll get
back to you.

Hi

For that you have to have associations between the users table and
countries as well as states table

Use the country id and state id as foreign keys in users table

By the way did you use collection_select to implement dropdrown .Did you
solve the previous problem
Please let me know

All the Best

Abhimanyu

Abhi M. wrote:

Hi

For that you have to have associations between the users table and
countries as well as states table

Use the country id and state id as foreign keys in users table

By the way did you use collection_select to implement dropdrown .Did you
solve the previous problem
Please let me know

All the Best

Abhimanyu

Hi Abhimanyu

See I have a users table, countries table and a states table. I am
taking foreign key country_id and state_id in users table.

I hav used collection_select to implement the drop downs. Shall I send
you the code to make it more clear?

Hi Ruchita

Yes Please send me the code. You can use the foreign keys as you
sent.Some times I wont be able to reply today . May be between morning
and evening we can have discussion.

Are you a student . I asked because If you are free other times we can
have discussion then. I will be leaving after a short while
THANKS
ABHIMANYU

Ruchita S. wrote:

Abhi M. wrote:

Hi,
I have given only some idea to you. Its not part of any code . You can
use collection select for drop down list
and if any country is selected then javascript function function
selectedData(selectedvalue) is called. In that function itself it will
call other action

function selectedData(selectedvalue)
{
var stateid = selectedvalue;
<%= remote_function(:update => ‘show’, :action =>
action_name,:with => “‘value=’+encodeURIComponent(stateid)”)%>
}

write the state drop down list in a partial file and render that file in
the above action you call

and also define a div called show in the view file of country drop down
and render the partial file here also.

Please try this

Okie… let me try to implement this. Thanks for your help. I’ll get
back to you.

Thanks a lot for your help. I have implemented the drop downs. Now the
problem is I have to save the data of the country and state in the users
table. I dont know how to save the country and state in the database
because i am fetching the data from the countries and states table but i
have to make it save in users table.

Please do let me know.
Ruchita
Thanks

Ruchita S. wrote:

Hi,

I mean i am not able to save data in users table.

Abhi M. wrote:

Hi Ruchita

Yes Please send me the code. You can use the foreign keys as you
sent.Some times I wont be able to reply today . May be between morning
and evening we can have discussion.

Are you a student . I asked because If you are free other times we can
have discussion then. I will be leaving after a short while
THANKS
ABHIMANYU

Hi

<%= collection_select(:country, :country_name, @countries, :id, :country_name, options = {:prompt => "– Select a Country –"}, html_options = { :onChange => "new Ajax.Updater('dd_cities', '/user/country_changed/' + this[this.selectedIndex].value, {asynchronous:true, evalScripts:true});" } ) %> State <%= render :partial => "states", :locals => { :country_id => @countries[0].id} %>

This is what i have written code in controller. I have made a
_states.rhtml.
Now i am able to save the data in my users table.

I am not a student. Tomorrow wud be my off, so plz if p’ble do let me
knw wat needs to be done?

Thanks,
Ruchita S…

Ruchita S. wrote:

Ruchita S. wrote:

Hi,

I mean i am not able to save data in users table.

Hi,

I am getting the following error when i click on save button:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.inject

Extracted source (around line #31):

28:

Country

29:
30: <%=
31: collection_select(:country, :country_name, @countries, :id,
:country_name,
32: options = {:prompt => “� Select a Country �”},
33: html_options =
34: {

Do let me know what needs to be done?

Thanks
Ruchita S…