Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Partial Based on Select Box - Rails 2.3.5

I've edited my request to hopefully be clearer. I need to render a partial dynamically based on a previous selection box.

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>


  ####  and here is where I need help:
  ####  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);
});
like image 321
Katie M Avatar asked Dec 07 '25 08:12

Katie M


1 Answers

one reminder first before we start. I'm not sure about this but I think request is a reserved word in rails.

JS

this just observes the dropdown and performs an ajax call

$(document).ready(function() {
  $('#request_product_id').change(function() {
    $.ajax({ url: '/products/' + this.value + '/form_partial' });
  });
});

ROUTES

nothing fancy here either. Just setting up a route where the ajax will go to when it is triggered

resources :products do
  get :form_partial, on: :member
end

CONTROLLER

we just fetch the product using :id which is passed from ajax

def form_partial
  @product = Product.find params[:id]
end

JS TEMPLATE

you need to create a form_partial.js.erb which will render the partial depending on the product. The code below appends the partial after the product_field div

 # app/views/products/form_partial.js.erb
 $('#product_partial').remove();
 <% if @product.id == 1 %>
   $('#product_field').after('<div id="product_partial"><%= escape_javascript render('partial1') %></div>');
 <% else %>
   $('#product_field').after('<div id="product_partial"><%= escape_javascript render('partial2') %></div>');
 <% end %>

UPDATE: for rails 2.x

we just need to change the routes and the js template in order for this to run on rails 2.x

ROUTES 2.x

map.resources :products, member: { form_partial: :get }

JS TEMPLATE 2.x

if I remember correctly, the file should be named form_partial.js.rjs. This will give you a page variable which you can use to add js.

 # app/views/products/form_partial.js.rjs
 page << "$('#product_partial').remove();"
 page << "<% if @product.id == 1 %>"
 page << "  $('#product_field').after('<div id="product_partial"><%= escape_javascript render('partial1') %></div>');"
 page << "<% else %>"
 page << "  $('#product_field').after('<div id="product_partial"><%= escape_javascript render('partial2') %></div>');"
 page << "<% end %>"
like image 176
jvnill Avatar answered Dec 08 '25 22:12

jvnill