Can anyone help me with this?  

On Wednesday, January 30, 2013 1:50:57 PM UTC-8, KT wrote:
>
> Hi all - hope you're well and having a great new year.  I have a Rails 
> 2.3.5 app with which I need some help.  I appreciate any guidance.  Thanks 
> in advance!
>
> REQUEST belongs to PRODUCT
> PRODUCT belongs to CATEGORY
> CATEGORY has many PRODUCTS
> PRODUCT has many REQUESTS
>
> User hits form: create_request.html.erb
> User selects a category, then the products select list is populated (like 
> Railscast 88 - dynamic select boxes)
>
> What I now need is to render different partial forms based on which 
> product is selected.  I suck at jquery.
>
> -------------------------------------------------
> *create_request.html.erb:*
>
> <%= javascript_include_tag "dynamic_products.js" %>
>
> <% form_for :request, :url => {:controller => :requests, :action => 
> :create_request, :id => params[:id]} do |f| %>
>
>   <label>Select Category:</label>
>   <%= select( "request", "category_id", Category.find( :all).collect { |c| 
> [c.name, c.id] })%><br>
>
>   <div id="product_field">
>      <label>Select Product:</label>
>      <%= select( "request", "product_id", Product.find( :all).collect { 
> |p| [p.name, p.id] })%><br>
>   </div>
>      
> *  #### 
>            here's where I need help, like so:
>            if request.product_id = 1, render partial _form1
>            if request.product_id = 2, render partial _form2
>   ####*
>   
>   <button  type="submit">Submit</button>
>
> <% 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 %>, <%= product.active %>));
>   products.sort()
> <% 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 && product[3] == 1) {
>       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);
> });
>
>

-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
--- 
You received this message because you are subscribed to the Google Groups "SD 
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to