# Ajax and dependent select lists

**URL:** <https://www.ruby-forum.com/t/ajax-and-dependent-select-lists/107527>\
**Category:** Rails\
**Created:** [June 22, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/ajax-and-dependent-select-lists/107527 "2007-06-22T16:34:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://www.ruby-forum.com/u/tom)\
**Post date:** [June 22, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/ajax-and-dependent-select-lists/107527/1 "2007-06-22T16:34:51Z")

</div>

Have been searching for a solution to this problem for a few weeks and  
have had no luck. I’m sure its a newbie mistake, but can’t figure out  
how to solve it.

My problem is that I have several dependent select lists that need to be  
populated from a database. The selected items then need to be saved  
back to the database. This is a medical application and basically, the  
user selects a cancer site in the top level select list which then  
generates a list of site-specific tumor stages based on the top level  
select. I can make the dynamically created select list, but can’t save  
the data from the form. The code snippet I’m posting doesn’t do exactly  
that. It just tries to print the selected item from the dynamically  
created list. I’m including the relevant parts of the views and  
controller.

Thanks for your help.

primary\_tumor\_controller.rb  
def new  
params[:user\_id] = “1”  
params[:patient\_id] = “1”  
@cancer\_sites = MedicalTerm.find\_all\_by\_semantic\_class(“cancer type”)  
@t\_stages = “”  
@primary\_tumor = PrimaryTumor.new  
end

def provide\_t\_stage  
unless params[:primary\_tumor\_tumor\_site].blank?  
@t\_stages = MedicalTerm.find\_all\_by\_semantic\_features("[tumor  
classification][#{params[:primary\_tumor\_tumor\_site]}][t stage]")  
render :partial=\>‘provide\_t\_stage’,  
:locals =\> {:t\_stages =\> @t\_stages}  
end  
end

def t\_stage\_selected  
render :partial =\> ‘t\_stage\_selected’,  
:locals =\> {:t\_stage\_selected =\>  
params[:primary\_tumor\_t\_stage]}  
end

new.rhtml

# Create Primary Tumor

\<% form\_for :primary\_tumor do |form| %\>

Tumor site  
\<%= collection\_select :primary\_tumor, :tumor\_site, @cancer\_sites, :name, :name %\>

\<%= "??" %\>

 \<%= "??" %\>
\<%= observe\_field 'primary\_tumor\_tumor\_site', :update =\> 't\_stage', :url =\> {:action =\> 'provide\_t\_stage', :only\_path =\> false}, :with =\> "'primary\_tumor\_tumor\_site=' + encodeURIComponent(value)" %\>

\<%= observe\_field ‘primary\_tumor\_t\_stage’,  
:update =\> ‘t\_stage\_selected’,  
:url =\> {:action =\> ‘t\_stage\_selected’, :only\_path =\>  
false},  
:with =\> “‘primary\_tumor\_t\_stage=’ +  
encodeURIComponent(value)” %\>

\<%= link\_to ‘Create’, :url =\> {:action =\> ‘create’} %\>  
\<% end %\>

\<%= link\_to ‘Show’, :action =\> ‘show’, :id =\> @primary\_tumor %\>

\_provide\_t\_stage.rhtml

T stage  
\<%= collection\_select :primary\_tumor, :t\_stage, @t\_stages, :name, :name %\>

\_t\_stage\_selected.rhtml  
\<%= @t\_stage\_selected %\>
