# Creating a Query and then a select box based off query results

**URL:** <https://www.ruby-forum.com/t/creating-a-query-and-then-a-select-box-based-off-query-results/153523>\
**Category:** Rails\
**Created:** [November 18, 2008, 11:41am UTC](https://www.ruby-forum.com/t/creating-a-query-and-then-a-select-box-based-off-query-results/153523 "2008-11-18T11:41:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 18, 2008, 11:41am UTC](https://www.ruby-forum.com/t/creating-a-query-and-then-a-select-box-based-off-query-results/153523/1 "2008-11-18T11:41:27Z")

</div>

Hello,

I am a newbie here and was hoping someone had some experience and  
suggestions on how to accomplish the below task:

I would like to figure out how to give my users a prepopulated list  
based off there selection. I am creating am application that users  
specific servers and would like to show them the list of suppliers who  
build these servers. For example, if they chose a server X200 the  
next box would then prepopulate its self with the supplier thats is  
responsible for building this server. Anyone have any idea how I would  
go about doing this. I have a query that pulls the data but I have no  
idea what my next steps should be.

Thank You,

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 18, 2008, 12:23pm UTC](https://www.ruby-forum.com/t/creating-a-query-and-then-a-select-box-based-off-query-results/153523/2 "2008-11-18T12:23:42Z")

</div>

Roughly you will change the view when the user selects a server.  
Use the onchange (not onclick) event of the select box  
to trigger the next view.  
Obviously that should be an Ajax call, since there is no  
need to update the whole page.

something similar, a category select with subcategories:

the view with the main category select:

\<select name=“category\_main” id=“category\_main” style=“float:left;  
width:200px;” onchange="\<%= remote\_function(:url =\>  
sub\_category\_depot\_product\_url(@product), :with =\> “‘category\_id=’ +  
this.value”) %\>"\>  
Kies een categorie  
\<%= options\_from\_collection\_for\_select(@categories, “id”,  
“title”, (@product.category ? @product.category.category\_id : 0)) %\>

the action to get the sub categories:

def sub\_category  
@product = Product.find(params[:id])  
@categories = Category.find(:all, :order =\> “title  
ASC”, :conditions =\> “category\_id = #{params[:category\_id]} AND  
published=1”)  
respond\_to do |format|  
format.js  
end  
end

the ajax response used by this action:

page.replace(:category\_sub, :partial =\> ‘depot/products/  
sub\_category\_select’, :locals =\> {:categories =\> @categories, :product  
=\> @product})

and the partial used by the ajax response:

\<%= options\_from\_collection\_for\_select(categories, “id”, “title”,  
product.category\_id) %\>

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 18, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/creating-a-query-and-then-a-select-box-based-off-query-results/153523/3 "2008-11-18T20:34:25Z")

</div>

Thanks, this pretty helpful. I am going through this step by step.Â Had  
no idea even how to begin this or even what to call it when try to find  
a solution

— On Tue, 11/18/08, Thorsten MÃ¼ller [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
From: Thorsten MÃ¼ller [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: [Rails] Re: Creating a Query and then a select box based off  
query results  
To: “Ruby on Rails: Talk” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Tuesday, November 18, 2008, 6:22 AM

Roughly you will change the view when the user selects a server.  
Use the onchange (not onclick) event of the select box  
to trigger the next view.  
Obviously that should be an Ajax call, since there is no  
need to update the whole page.

something similar, a category select with subcategories:

the view with the main category select:

\<select name=“category\_main” id=“category\_main”  
style=“float:left;  
width:200px;” onchange=“\<%= remote\_function(:url =\>  
sub\_category\_depot\_product\_url(@product), :with =\>  
“‘category\_id=’ +  
this.value”) %\>”\>  
Kies een categorie  
\<%= options\_from\_collection\_for\_select(@categories,  
“id”,  
“title”, (@product.category ? @product.category.category\_id : 0))  
%\>

the action to get the sub categories:

def sub\_category  
@product = Product.find(params[:id])  
@categories = Category.find(:all, :order =\> “title  
ASC”, :conditions =\> “category\_id = #{params[:category\_id]} AND  
published=1”)  
respond\_to do |format|  
format.js  
end  
end

the ajax response used by this action:

page.replace(:category\_sub, :partial =\> ‘depot/products/  
sub\_category\_select’, :locals =\> {:categories =\> @categories,  
:product  
=\> @product})

and the partial used by the ajax response:

\<%= options\_from\_collection\_for\_select(categories, “id”,  
“title”,  
product.category\_id) %\>
