Hi KT,
  You can go two paths here.  In path one you render all of the potential 
product forms, and show and hide them as needed.  If there are only a few, this 
may be the way to go.  If there are tons, it will make the page unnecessarily 
large.  In path two you can make an Ajax call to the server and get the proper 
form returned based on the product chosen.  This means a separate round trip to 
the server, but handles a larger quantity w/o making the rendered DOM huge.

path1 would look something like this:

<% @products.each do |product| %>
  <%= div_for :product, :class => 'hidden' do %>
    <%= render :partial => "partial_form#{product.id}" %>
  <% end %>
<% end %>

- You need css for class hidden to keep it from being shown (aka display:none)
- In your categorySelected function, you'd simply do something like 
$('.products).hide(); $('#product_'+selected_id).show();

In path two you need a controller action which responds to a JS request and 
renders back the HTML partial for the selected product.  You send product_id as 
a param and just do render :partial => "partial_form_#{params[:product_id]}".  
Your categorySelected function would then do a GET request and replace the HTML 
that holds the form with the HTML returned from Rails.

Best,
Rob

On Jan 30, 2013, at 1:50 PM, KT <[email protected]> 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.
>  
>  

-- 
-- 
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