Forum: Ruby on Rails Dynamic SelectBoxes/DropDownLists

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
65c4c8c229fb099387c04c454ce41cdf?d=identicon&s=25 milos kelemen (Guest)
on 2005-12-12 11:35
(Received via mailing list)
Hi,

I'd like to implement in my project dynamic dropdown lists.
Example:
user will select one option from the parent dropdown list, lets say
it'll be COUNTRY and in the child dropdown will be generated all CITIES
tied with this selected country.

I could then have relevant info in my form. Country and one of its city.


I know, this is not much abour RoR, however it has something to do w/
Ajax. I really like autocomplete feature in RoR and I'm sure this
dynamic dropdowns should be done easy in Ajax too.

any clues..?

thanks and have a great day

-> keli (milos kelemen)
6b7c871c2036a41f0253580074b406cc?d=identicon&s=25 Nick C. (n1c)
on 2005-12-12 12:27
milos kelemen wrote:>
> I'd like to implement in my project dynamic dropdown lists.
> Example:
> user will select one option from the parent dropdown list, lets say
> it'll be COUNTRY and in the child dropdown will be generated all CITIES
> tied with this selected country.

You could use an observe_field to monitor the choice in the first select
box, as in this example:
http://www.roryhansen.ca/?p=9

This is something I'm currently experimenting with myself - will let you
know how it goes.
65c4c8c229fb099387c04c454ce41cdf?d=identicon&s=25 milos kelemen (Guest)
on 2005-12-12 13:23
(Received via mailing list)
Nick Ce wrote:
> box, as in this example:
> http://www.roryhansen.ca/?p=9
>
> This is something I'm currently experimenting with myself - will let you
> know how it goes.
>
tnak you nick,

I'll let you know how it goes,


-> keli
65c4c8c229fb099387c04c454ce41cdf?d=identicon&s=25 milos kelemen (Guest)
on 2005-12-12 14:44
(Received via mailing list)
Nick Ce wrote:
> box, as in this example:
> http://www.roryhansen.ca/?p=9
>
> This is something I'm currently experimenting with myself - will let you
> know how it goes.
>
Nick,


thanks for your help again...

this script works as it should
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2005-12-12 15:32
(Received via mailing list)
hi,
someone asked the exact same question here ..see this
http://article.gmane.org/gmane.comp.lang.ruby.rail...
and this
http://article.gmane.org/gmane.comp.lang.ruby.rail...
Vivek
6b7c871c2036a41f0253580074b406cc?d=identicon&s=25 Nick C. (n1c)
on 2005-12-12 23:52
milos kelemen wrote:
> Nick,
>
> thanks for your help again...
>
> this script works as it should

No problem - glad yours works ok - still having problems with mine :(
I decided to go down the partials route as used in the posts that Vivek
linked to, here
http://article.gmane.org/gmane.comp.lang.ruby.rail... and
here http://article.gmane.org/gmane.comp.lang.ruby.rail...

At present I have this: http://www.rafb.net/paste/results/YxkO1V47.html

Unfortunately, when I make a choice on the first select, no content
appears within the person_list div - can anyone see anything wrong at
all?
765b89293663740d0cf5dab673311a90?d=identicon&s=25 Rory Hansen (Guest)
on 2005-12-13 01:23
(Received via mailing list)
Are you using Firefox? If so, make sure that the ID of the select box is
actually job_company_id. I'm not familiar with the output of
collection_select, but it may output only the NAME attribute and not the
ID
unless you explicitly tell it to.
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2005-12-13 06:42
(Received via mailing list)
In the post which I linked to..I didnt understand the use of :object ..I
know of :collection which calls the template repeatedly..whats with this
:object ? does anyone here have an idea?

I saw rory's solution.. easier to follow..Thanks for the tip.
Vivek
6b7c871c2036a41f0253580074b406cc?d=identicon&s=25 Nick C. (n1c)
on 2005-12-13 10:16
Rory Hansen wrote:
> Are you using Firefox? If so, make sure that the ID of the select box is
> actually job_company_id. I'm not familiar with the output of
> collection_select, but it may output only the NAME attribute and not the
> ID unless you explicitly tell it to.

Thanks for the reply Rory. I'm actually using Camino, and the
collection_select generates this:

<p><label for="job_company_id">Client</label><br/>
<select id="job_company_id" name="job[company_id]">
<option value="1">company A</option>
<option value="2">company B</option>

which looks ok to me.

Vivek wrote:
> In the post which I linked to..I didnt understand the use of :object ..I
> know of :collection which calls the template repeatedly..whats with this
> :object ? does anyone here have an idea?

From the Agile Rails book: "The :object parameter identifies an object
to be passed into the partial. This object will be available within the
template via a local variable with the same name as the template." Not
sure I understand this fully - does my error lie with the naming of the
object variable perhaps? Still can't get it to work as expected. The DIV
I'm trying to update is nested within a form; will that make any
difference? http://www.rafb.net/paste/results/YxkO1V47.html
6b7c871c2036a41f0253580074b406cc?d=identicon&s=25 Nick C. (n1c)
on 2005-12-13 10:34
Nick C wrote:
> http://www.rafb.net/paste/results/YxkO1V47.html

Solved it - it was a syntax error - one measly single quote missing from
:with => "'job_company_id='+value"

The partial method in conjunction with collection_select certainly seems
a tidy way of doing dynamic drop downs.

Thanks to all those who chipped in.
This topic is locked and can not be replied to.