# AJAX problem

**URL:** <https://www.ruby-forum.com/t/ajax-problem/129479>\
**Category:** Rails\
**Created:** [February 18, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/ajax-problem/129479 "2008-02-18T23:01:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierreact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@pierreact](https://www.ruby-forum.com/u/pierreact)\
**Post date:** [February 18, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/ajax-problem/129479/1 "2008-02-18T23:01:22Z")

</div>

Hello everyone 🙂

I have this ajax issue, I’d be glad if someone would be willing to  
help, thanks 🙂

What I try to do:  
Have a select box with a list of countries.  
When the country select box value is changed, have the state box  
appear.  
Up to this point, I got this successfully.

When the state box gets changed, have the city box appear for  
selection. This is what I couldn’t acheive…

How I coded it:  
In my rhtml page I got this code:

\<%= select “assoc”, “country\_id”, @country\_options, {:include\_blank =\>  
true} %\>  
\<%= observe\_field(:assoc\_country\_id,  
:frequency =\> 0.5,  
:update =\> :states,  
:url =\> { :action =\> :show\_states\_select },  
:with =\> “‘country\_id=’ + value”) %\>

In show\_states\_select.rhtml, I got this:  
\<%= select ‘assoc’, ‘state\_id’, @state\_options, {:include\_blank =\>  
true} %\>

\<%= observe\_field(:assoc\_state\_id,  
:frequency =\> 0.5,  
:update =\> :cities,  
:url =\> { :action =\> :show\_cities\_select },  
:with =\> “‘state\_id=’ + value”) %\>

And show\_cities\_select.rhtml:  
\<%= select ‘user’, ‘city\_id’, @city\_options, {:include\_blank =\> true}  
%\>

I think it does not appear because maybe the second observe\_field  
comes from ajax request and maybe is not interpreted, I don’t know, if  
someone could help me, I’d appreciate…

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![pierreact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@pierreact](https://www.ruby-forum.com/u/pierreact)\
**Post date:** [February 19, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/ajax-problem/129479/2 "2008-02-19T13:37:10Z")

</div>

Come on, no idea for this ?

---

<div class="post-metadata">

**Author:** ![pierreact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@pierreact](https://www.ruby-forum.com/u/pierreact)\
**Post date:** [February 19, 2008, 2:01pm UTC](https://www.ruby-forum.com/t/ajax-problem/129479/3 "2008-02-19T14:01:59Z")

</div>

(My apologies if this gets double-posted - I sent it last night but  
haven’t  
gotten any indication that it was received. It might be something weird  
with GMail, however)

* * *

Hello, everyone.

I’m currently working on a politically-themed social networking site  
that I  
hope will grow in the near future. I’ve decided to use (obviously) Ruby  
on  
Rails to write the site, although I come from an [ASP.NET](http://ASP.NET) background and  
this  
is my first real foray into RoR and anything outside of the MS world  
(I’ve  
dabbled very little in Linux, so I know the basics but not much else).

The issue I’m running into, however, is that I want to have an  
integrated  
blogging module that allows for multiple blogs by multiple users, each  
with  
different URLs - a core feature of the site is that it will host various  
related blogs by different people, hopefully with subdomains such as  
[johndoe.mysite.com](http://johndoe.mysite.com) (which would redirect to something like  
[mysite.com/blogs/johndoe](http://mysite.com/blogs/johndoe)). I’d also like to have integrated forums, so  
that  
when a user joins the site, their session spans to the forums, and for  
posting comments to blogs (we only want to allow registered users to  
post  
comments), and the like. The integrated forums is a must because users  
can  
have special icons based on information in their profile (if they own a  
business, for example, and want to associate with the site), but other  
than  
that there are no specific requirements outside of a normal forum.

My web host (HostingRails) has some options built-in, but they’re all  
PHP  
based and I’m finding that they don’t really integrate well with the RoR  
application (one of the PHP boards especially, since it uses a similar  
URL  
method to Rails, and thus Rails tries to parse it, only to fail and give  
me  
a 404); I could use them in a pinch, but I’d really like a unified look  
and  
feel to the site. As I said, I’m relatively new to RoR development (I’ve  
read through the Agile Web D. With Rails book twice now) so,  
while  
I could probably try my hand at writing custom functionality, I figure  
I’d  
take a chance and see if there’s anything that I can use as the base  
first.

Is there anything out there that would fulfil my needs, or would I be  
better  
off rolling up my sleeves and coding my own? I have an idea of what it  
would  
take, I’m just not confident enough in my RoR skills to think I could do  
a  
very good job of it, as I’m still pretty much a newbie.

I’m hoping that someone can point me in the right direction, or at least  
offer some suggestions I can look into. Many thanks!

- Wayne M  
Co-Founder, [LibertyTruth.org](http://LibertyTruth.org)

No virus found in this outgoing message.  
Checked by AVG Free Edition.  
Version: 7.5.516 / Virus Database: 269.20.7/1285 - Release Date:  
2/18/2008  
5:50 AM

---

<div class="post-metadata">

**Author:** ![pierreact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@pierreact](https://www.ruby-forum.com/u/pierreact)\
**Post date:** [February 19, 2008, 11:42pm UTC](https://www.ruby-forum.com/t/ajax-problem/129479/4 "2008-02-19T23:42:55Z")

</div>

Check out [railscasts.com](http://railscasts.com). Ryan B. did an excellent cast on exactly  
this topic a few weeks ago (no. 88 - Dynamic Select Menus). Better  
than AJAX, since it is all processed client-side. (You’d also be able  
to use the caching methods, since your available options aren’t going  
to change very often.)

---

<div class="post-metadata">

**Author:** ![pierreact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@pierreact](https://www.ruby-forum.com/u/pierreact)\
**Post date:** [February 19, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/ajax-problem/129479/5 "2008-02-19T16:27:10Z")

</div>

why not have an  
onChange=“showState();”  
on the country list.

then in some JS have  
function showState(){  
// use CSS to show the box.  
document.getElementById(‘user\_city\_id’).style.display=‘block’;  
}  
to make it “appear”

and you can “hide” the city box by adding some JS at the bottom of the  
page.  
// use CSS to hide the box.  
document.getElementById(‘user\_city\_id’).style.display=‘none’;  
This way if the user does not have JS turned on they will still see  
the city box.

John I.

---

<div class="post-metadata">

**Author:** ![pierreact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@pierreact](https://www.ruby-forum.com/u/pierreact)\
**Post date:** [February 20, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/ajax-problem/129479/6 "2008-02-20T19:57:45Z")

</div>

Unless I misunderstand the problem, the issue is that he wants to  
dynamically populate the city and state selects based on the user’s  
choices. Pick country A and you get a list of n States populated into  
the state select. Pick state B from the newly visible state select  
and you get m cities in that state populated into the newly visible  
city select.

@pierre – I think that IE gets very cranky about executing JS that  
was not part of the original page. You may find that you need to  
compromise here. Create the selects and the observers and hide their  
containing divs. Your AJAX method can then either replace the  
previously empty select OR add the options to it (rather than  
rendering a new select with options and its observer). The return js  
would finish up by making the containing div visible.

HTH,  
AndyV
