I’ve implemented Ryan B.'s Dynamic Select modules as done here:
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…
Can you show the code in the corresponding views and controllers?
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" })%>
Do I need to create another javascript to populate descriptions? I’m
stuck… please help!