Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:29:54+00:00 2026-06-18T08:29:54+00:00

I’ve edited my request to hopefully be clearer. I need to render a partial

  • 0

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);
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:29:56+00:00Added an answer on June 18, 2026 at 8:29 am

    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 %>"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a small JavaScript validation script that validates inputs based on Regex. I
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.