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

  • Home
  • SEARCH
  • 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 7935585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:58:03+00:00 2026-06-03T21:58:03+00:00

I have a Rails nested form with the child fields that contain dynamic select

  • 0

I have a Rails nested form with the child fields that contain dynamic select menus:
–category (filter menu)
–products

I can get a single fieldset to work, but I have hack the products menu (by using a “backup” collection_select in main form that is DOM ready) because I couldn’t get the .html() to load the select data on a dynamic field. I’m using the revised Railscast #88 as the template code.

The real issue is that only the first select menu combo work — when I select the category menu in additional fieldsets, all menus fail. I tried adding a (this) method, but could get it to work.

Any insights greatly appreciated!

Relevent js.coffee:

products = $('.products-backup').html()
$('form').on 'change', '.category', (event) ->
category = $('.category :selected').text()
escaped_category = category.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
options = $(products).filter("optgroup[label='#{escaped_category}']").html()
if options
    $('.products').html(options)
else
    $('.products').empty()

Relevent rails html.erb partial – note, I assigned :class names to each select menu:

<fieldset>
<div class="field">
<%= f.collection_select :category_id, Category.all, :id, :name, { prompt: 'Select' }, 
{ :class => "category" } %> 
</div>
<div class="field">
<%= f.grouped_collection_select :product_id, Category.order(:name), :products, 
:name, :id, :name, { prompt: 'Select' }, { :class => "products" } %>
</div>
#...more fields
</fieldset>
  • 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-03T21:58:04+00:00Added an answer on June 3, 2026 at 9:58 pm

    @Jeff, you my want something like this:

    var $products_backup = $('#products_backup');
    $('form').on('click', '.category', function() {
        var $category = $(this);
        if (!$category.is(":checked")) {
            return;
        }
        var escaped_category = $category.val().replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
        var $options = $products_backup.find("optgroup[label='" + escaped_category + "']").find("option");
        //$('#products').empty().append('<option value="">Select an item</option>').append($options.clone()).val("");
        $category.closest('fieldset').find('.products').empty().append('<option value="">Select an item</option>').append($options.clone()).val("");
    });
    

    See :

    • DEMO (original)
    • DEMO (update)

    As you will see I guessed that the .category elements might be radio buttons. If they are not, then the code will need to be adjusted to a greater or lesser extent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested form in a rails view that is called like this
I have a one to many relationship with a child form that is nested
I have a three-level multi-nested form in Rails. The setup is like this: Projects
I have a nested form in which I would like the form fields to
I have a nested form in a Rails 3.1 app, using Cocoon gem. In
I'm attempting to get over the hurdle of dynamic form fields in Rails --
I'm following this rails cast to create a nested form: http://railscasts.com/episodes/196-nested-model-form-part-1 I have a
I have a rails 3.2.3, I have a form with two nested models, when
I have a rails 3 form with nested attributes. So, for example, the parent
Let's assume you are using Rails, and have a form with nested models; for

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.