Implementing countries and states drop down

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
please tell me the fields of the country,state and users table
collection_select(:country, :country_name, @countries, :id,
:country_name,

here the :country_name after :id is it a field in countries table?

Hi Abhimanyu,

The fields of countries table are id and country_name. The fields of
states table are id, country_id and state_name. The fields of users
table are id, login, first_name,and so on including country_id and
state_id.

Now please do let me know what needs to be done?

Yes country_name is a field in countries table.

you can use observe filed

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
please tell me the fields of the country,state and users table
collection_select(:country, :country_name, @countries, :id,
:country_name,

here the :country_name after :id is it a field in countries table?

Hi
the code you sent . is this in a partial file?

Abhi M. wrote:

Hi Ruchita

Try to use submit_to_remote like this

<%= collection_select(:country,":country_name,@countries, :id,:country_name,{:onchange => "new Ajax.Updater('dd_cities', '/user/country_changed/' + this[this.selectedIndex].value, {asynchronous:true, evalScripts:true});") %> <%= submit_to_remote("save", "Save", :url => {:action =>"action name", :update => "dd_cities") %>

Hi,

Its not working. Is there some other method to implement it?

Hi Ruchita

Try to use submit_to_remote like this

<%= collection_select(:country,":country_name,@countries, :id,:country_name,{:onchange => "new Ajax.Updater('dd_cities', '/user/country_changed/' + this[this.selectedIndex].value, {asynchronous:true, evalScripts:true});") %> <%= submit_to_remote("save", "Save", :url => {:action =>"action name", :update => "dd_cities") %>

Hi
is it syntax error

Abhi M. wrote:

Hi
is it syntax error

yes, td><%= collection_select(:country,":country_name,@countries,

:id,:country_name,{:onchange => “new Ajax.Updater(‘dd_cities’,
‘/user/country_changed/’ + this[this.selectedIndex].value,
{asynchronous:true, evalScripts:true});”) %>

You havnt closed the double quotes starting before country_name. I hav
tried all the options but it’s not working. Moreover, I am not getting
where to use the second line of code u have send?
My code is as follows:

<%= 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} %>

Now tell me how to implement dt stuff.

Thanks,
Ruchita

Abhi M. wrote:
Hi
sorry there is no double quotes there
the second line of code is to save the data that you can write after
collection_select itself . But if you want to save states also

be within form tag

Hi

<%= collection_select(:country,":country_name,@countries, :id,:country_name,{:onchange => "javascript:selectedData(this.options[selectedIndex].value)"}") %> <%= submit_to_remote("save", "Save", :url => {:action =>"action name"}, :update => "dd_cities") %>

There should be a function “selectedData(selectedIndex)” and inside the
function you should have following line
var country_id = selectedIndex
<%= remote_function(:update => ‘dd_cities’, :url => {:action =>“action
name”},:with => “‘value=’+encodeURIComponent(country_id )”)%>

and

I forgot to put “}” earlier

Abhi M. wrote:

Abhi M. wrote:
Hi
sorry there is no double quotes there
the second line of code is to save the data that you can write after
collection_select itself . But if you want to save states also

be within form tag

Hi,

It’s not working and I have to get it done today only.

My advice is to use observer field, instead using javascript codes.
its really messy

On Oct 29, 10:03 am, Abhi M. [email protected]

Check this link for observer field, same like that you can do it for
select.

http://www.recentrambles.com/pragmatic/view/59

Hi

If you are still using javascript send me the updated code or else if
you are in hurry then try as Mr Bala has suggested for some time(say 1
hr max) Because its not good to change the flow often . Also javascript
is not that much messy if get used to it. I hope I have not confused you
in our discussions all along

Hi
if drop down is working fine
Please use breakpoint in the controller to check that whether the values
params[:country][:country_name] and
params[:state][:state_name] and let me know

Abhi M. wrote:

Hi
if drop down is working fine
Please use breakpoint in the controller to check that whether the values
params[:country][:country_name] and
params[:state][:state_name] and let me know

Hi,

Both values are coming right. But the control is not going after
if @user.save… and therefore no data is being saved in the
database. Let me know if if i have done some mistake.

Abhi M. wrote:

Hi

If you are still using javascript send me the updated code or else if
you are in hurry then try as Mr Bala has suggested for some time(say 1
hr max) Because its not good to change the flow often . Also javascript
is not that much messy if get used to it. I hope I have not confused you
in our discussions all along.

Hi,

I am really very confused. I am using javascript code only… dont want
to change the flow. I am sending you the code of view as well as that of
controller. I know what the problem is but really dont hav an idea wat
to do?

The name of view is new_client.rhtml.

<%= javascript_include_tag :defaults %>

<%= show_flashes :notice, :warning %>

Add New Client

Login
Password
Confirm Password
First Name
Last Name
Country
<%= 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} %>
Phone

user controller:

def new_client
@user = User.new
@countries = Country.find :all
@states = State.find :all
@countries = Country.find(:all, :order => ‘country_name’)
@states = State.find_by_state_name(params[:id])
end

def create_client
@user = User.new(params[:user])
@user.country_id=params[:country][:country_name]
@user.state_id=params[:state][:state_name]
if @user.save
redirect_to :action => ‘list_user’
else
render :action => ‘new_client’
end
end

Drop downs are working fine but am not able to save data in users table.

Thanks a lot,
Ruchita

Abhi M. wrote:

Try removing
@user = User.new from action new_client and replace
@user = User.new(params[:user]) by @user = User.new

Still not working. Again the control is not going after if @user.save.
What should be done?

Try removing
@user = User.new from action new_client and replace
@user = User.new(params[:user]) by @user = User.new

Abhi M. wrote:

Hi tell me how you know its not getting saved did you checked db.
refresh it and check . Here maybe
“if” statement is not working

Yes i have checked the database. Data is not getting saved.