# Select item based on previous select list selection?

**URL:** <https://www.ruby-forum.com/t/select-item-based-on-previous-select-list-selection/58984>\
**Category:** Rails\
**Created:** [April 27, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/select-item-based-on-previous-select-list-selection/58984 "2006-04-27T16:26:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [April 27, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/select-item-based-on-previous-select-list-selection/58984/1 "2006-04-27T16:26:00Z")

</div>

So I have a select list with a list of items. When a user selects an  
item, another select list should have its default selected item set to a  
specific item (the default associated with the first select list).

This is in a form to create a new task. The first select list is a list  
of parent tasks. When one selects the parent task, the clients select  
list must default to the same client as its parent task.

I’ve come across several part examples using remote\_function or rjs, but  
my rails newbieness is preventing me from understanding those  
completely. Does anyone have complete code or a suggestion as to how to  
accomplish this.

This is my existing code:  
\<%= select (‘task’, ‘parent\_id’, @parents.collect {|p| [p.title, p.id]  
}, { :include\_blank =\> true }) %\>  
\<%= select (‘task’, ‘client\_id’, @clients.collect {|c| [c.name, c.id]  
}) %\>

Thx  
N.

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [April 27, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/select-item-based-on-previous-select-list-selection/58984/2 "2006-04-27T17:52:11Z")

</div>

On Apr 27, 2006, at 7:26 AM, Nick C. wrote:

> I’ve come across several part examples using remote\_function or  
> \<%= select (‘task’, ‘client\_id’, @clients.collect {|c| [ c.name,  
> c.id ]  
> }) %\>

Nick-

```
Here is an example of how you can get started doing this:

```

— In the view file ----

\<%= select (‘task’, ‘parent\_id’, @parents.collect {|p| [ p.title,  
p.id ]},  
{ :include\_blank =\> true,  
:onchange =\> remote\_function( :with =\>  
“‘parent\_id=’+value”,  
:loading =\> “Element.show(‘loading-  
indicator’)”,  
:url =\> { :action  
=\> :select\_with\_ajax } )) %\>}) %\>  
\<%= image\_tag(‘indicator.gif’,  
:id =\> ‘loading-indicator’,  
:style =\> ‘display:none;’ ) -%\>

— In the controller action----

def select\_with\_ajax  
@clients = Client.find(:all, :conditions =\> [“parent\_id = ?”,  
params[:parent\_id])  
render :update do |page|  
page.replace\_html(‘next\_select’, :partial =\>  
‘second\_select’, :collection =\> @clients  
page.hide(‘loading-indicator’)  
end  
end

The loading indicator gif is one of those little spinner animated gif  
files. You might need to change the @client=Client.find line to fetch  
whatever it is you want to populate the next select box with. And you  
will need a partial called \_second\_select.rhtml that has the code for  
the second select box.

Hope that gets you over the hump.

Cheers  
-Ezra

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [April 30, 2006, 7:49am UTC](https://www.ruby-forum.com/t/select-item-based-on-previous-select-list-selection/58984/3 "2006-04-30T07:49:45Z")

</div>

Hi Ezra

The code makes sense, but its not working as expected. While I do get  
the indicator image showing, the select box never updates. This is the  
code:

In the view:-  
\<%= select (‘task’, ‘parent\_id’, @parents.collect {|p| [ p.title, p.id  
]},  
{ :include\_blank =\> true},  
:onchange =\> remote\_function ( :with =\>  
“‘parent\_id=’+value”,  
:loading =\>  
“Element.show(‘loading-indicator’)”,  
:url =\> { :action =\>  
:select\_with\_ajax } )) %\>  
\<%= image\_tag(‘indicator.gif’,  
:id =\> ‘loading-indicator’,  
:style =\> ‘display:none;’ ) -%\>

\<%= render :partial =\> 'client\_list' %\>

In the controller:-  
def select\_with\_ajax  
@clients = Client.find(:all,  
:conditions =\> [“parent\_id = ?”,  
params[:parent\_id]],  
:joins =\> [:tasks])  
render :update do |page|  
page.replace\_html(‘task\_client’, :partial =\> ‘parent\_client\_list’,  
:collection =\> @clients)  
page.hide(‘loading-indicator’)  
end  
end

And the partial \_parent\_client\_list.rhtml looks like:  
\<%= select (‘task’, ‘client\_id’, @clients.collect {|c| [c.name, c.id]  
}, { :include\_blank =\> false }) %\>

What am I missing?

thx  
n.

Ezra Z. wrote:

> On Apr 27, 2006, at 7:26 AM, Nick C. wrote:
> 
> > I’ve come across several part examples using remote\_function or  
> > \<%= select (‘task’, ‘client\_id’, @clients.collect {|c| [ c.name,  
> > c.id ]  
> > }) %\>
> 
> Nick-
> 
> Here is an example of how you can get started doing this:
> 
> — In the view file ----
> 
> \<%= select (‘task’, ‘parent\_id’, @parents.collect {|p| [ p.title,  
> p.id ]},  
> { :include\_blank =\> true,  
> :onchange =\> remote\_function( :with =\>  
> “‘parent\_id=’+value”,  
> :loading =\> “Element.show(‘loading-  
> indicator’)”,  
> :url =\> { :action  
> =\> :select\_with\_ajax } )) %\>}) %\>  
> \<%= image\_tag(‘indicator.gif’,  
> :id =\> ‘loading-indicator’,  
> :style =\> ‘display:none;’ ) -%\>
> 
> — In the controller action----
> 
> def select\_with\_ajax  
> @clients = Client.find(:all, :conditions =\> [“parent\_id = ?”,  
> params[:parent\_id])  
> render :update do |page|  
> page.replace\_html(‘next\_select’, :partial =\>  
> ‘second\_select’, :collection =\> @clients  
> page.hide(‘loading-indicator’)  
> end  
> end
> 
> The loading indicator gif is one of those little spinner animated gif  
> files. You might need to change the @client=Client.find line to fetch  
> whatever it is you want to populate the next select box with. And you  
> will need a partial called \_second\_select.rhtml that has the code for  
> the second select box.
> 
> Hope that gets you over the hump.
> 
> Cheers  
> -Ezra

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [May 3, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/select-item-based-on-previous-select-list-selection/58984/4 "2006-05-03T19:10:59Z")

</div>

On Apr 29, 2006, at 11:39 PM, Nick C. wrote:

> > the indicator image showing, the select box never updates. This is  
> > the  
> > code:

Nick -

```
The best way to debug these ajax pages is with firefox and the

```

firebug plugin. It lets you examine the ajax calls and returns from  
the server so you can see exactly what the browser is getting back  
from your ajax call. The thing is when you use rjs, the text returned  
from the ajax action is eval’ed as javascript. If its not valid  
javascript then it won’t appear to do anything.

```
SO go get that plugin and see whats coming back when it seems like

```

its not returning. Also you can look at your development.log and you  
can see the ajax requests. Check there to see if the select\_with\_ajax  
is getting called or not.

-Ezra

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [April 30, 2006, 8:39am UTC](https://www.ruby-forum.com/t/select-item-based-on-previous-select-list-selection/58984/5 "2006-04-30T08:39:44Z")

</div>

I don’t think “select\_with\_ajax” is ever getting called, because no  
matter what I put inside that function, it never happens.

n.

Nick C. wrote:

> Hi Ezra
> 
> The code makes sense, but its not working as expected. While I do get  
> the indicator image showing, the select box never updates. This is the  
> code:
