# Dynamic Selects - Dynamic View

**URL:** <https://www.ruby-forum.com/t/dynamic-selects-dynamic-view/188046>\
**Category:** Rails\
**Created:** [May 4, 2010, 8:31pm UTC](https://www.ruby-forum.com/t/dynamic-selects-dynamic-view/188046 "2010-05-04T20:31:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KTKTKT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8797f3/32.png) [@KTKTKT](https://www.ruby-forum.com/u/KTKTKT)\
**Post date:** [May 4, 2010, 8:31pm UTC](https://www.ruby-forum.com/t/dynamic-selects-dynamic-view/188046/1 "2010-05-04T20:31:25Z")

</div>

I’ve implemented Ryan B.'s Dynamic Select modules as done here:

> **[\#88 Dynamic Select Menus - RailsCasts](http://railscasts.com/episodes/88-dynamic-select-menus)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

I have a Category select (hardware, software, media…)  
Based on the Category, I have a Product select (if hardware is chosen,  
then PC, Monitor, Printer…)

That all works.

But now I would like to show the product.description below the  
selected Product.

HELP!??!?!

Thanks for any input…

---

<div class="post-metadata">

**Author:** ![KTKTKT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8797f3/32.png) [@KTKTKT](https://www.ruby-forum.com/u/KTKTKT)\
**Post date:** [May 5, 2010, 4:57am UTC](https://www.ruby-forum.com/t/dynamic-selects-dynamic-view/188046/2 "2010-05-05T04:57:42Z")

</div>

Can you show the code in the corresponding views and controllers?

---

<div class="post-metadata">

**Author:** ![KTKTKT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8797f3/32.png) [@KTKTKT](https://www.ruby-forum.com/u/KTKTKT)\
**Post date:** [May 5, 2010, 4:51pm UTC](https://www.ruby-forum.com/t/dynamic-selects-dynamic-view/188046/3 "2010-05-05T16:51:14Z")

</div>

Thanks and sure thing:

javascripts controller:

def dynamic\_products  
@products = Product.find(:all)  
end

* * *

dynamic\_products.js.erb:

var products = new Array();  
\<% for product in @products -%\>  
products.push(new Array(\<%= product.category\_id %\>, ‘\<%=h  
product.name %\>’, \<%= product.id %\>));  
\<% end -%\>

function categorySelected() {  
category\_id = $(‘request\_category\_id’).getValue();  
options = $(‘request\_product\_id’).options;  
options.length = 1;  
products.each(function(product) {  
if (product[0] == category\_id) {  
options[options.length] = new Option(product[1], product[2]);  
}  
});  
if (options.length == 1) {  
$(‘product\_field’).hide();  
} else {  
$(‘product\_field’).show();  
}  
}

document.observe(‘dom:loaded’, function() {  
categorySelected();  
$(‘request\_category\_id’).observe(‘change’, categorySelected);  
});

* * *

view:

**Select Category:\***  
 \<%= select( "request", "category\_id", Category.find( :all, :order =\> "name" ).collect { |c| [c.name, c.id] }, { :include\_blank =\> true, :order =\> "name" })%\>

**Select Product/Service:\***  
 \<%= select( "request", "product\_id", Product.find( :all, :order =\> "name" ).collect { |p| [p.name, p.id] }, { :include\_blank =\> true, :order =\> "name" })%\>

* * *

---

<div class="post-metadata">

**Author:** ![KTKTKT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8797f3/32.png) [@KTKTKT](https://www.ruby-forum.com/u/KTKTKT)\
**Post date:** [May 6, 2010, 5:18pm UTC](https://www.ruby-forum.com/t/dynamic-selects-dynamic-view/188046/4 "2010-05-06T17:18:51Z")

</div>

Do I need to create another javascript to populate descriptions? I’m  
stuck… please help!
