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 9186469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:32:08+00:00 2026-06-17T19:32:08+00:00

I have a model ( Listing ) that has many, many attributes (say 25).

  • 0

I have a model (Listing) that has many, many attributes (say 25).

I would like to create an AJAX form that is dynamic based on the input in the form.

Meaning that if a user chooses type=b, they will see other fields that they should see and not ones they shouldn’t.

Ideally they should load immediately, with the right content from the db – i.e. in an AJAXy way.

What’s the best way to approach this?

Thanks.

Edit: Here is an example of my _form.html.erb. You will notice that I have included 2 if statements that indicate which fields should show if the property_type value chosen is one of those specified:

<%= simple_form_for(@listing) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :headline %>
    <%= f.input :price %>    
    <%= f.association :property_type %>

    If property_type == "coop"

    <%= f.input :maintenance %>
    <%= f.input :coop_deductible %>
    <%= f.input :flip_tax %>

    if property_type == "condo"

    <%= f.input :common_charges %>
    <%= f.input :taxes %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

Edit 2:

This is the Rails form helper that I used – powered by the gem Simple_Form:

<%= simple_form_for @listing, :defaults => { :wrapper_html => { :class => 'form-horizontal' } } do |f| %>
  <%= f.error_notification %>
  <fieldset>
    <%= f.association :listing_category, :label => "Category: ", :label_html => { :class => "control-label"}, :wrapper_html => { :class => "controls"} %>

    <div style="display: none;" data-show-for="Exclusive">
        <%= f.association :listing_type %>
      <%= f.association :user %>
      <%= f.association :boro %>
    </div>

    <div style="display: none;" data-show-for="Open">
      <%= f.association :neighborhood %>    
      <%= f.association :building %>
      <%= f.association :term %>
      <%= f.association :property_type %>
    </div>

    <div class="form-actions">
    <%= f.button :submit, :class => "btn btn-primary" %>
    <!-- <button type="submit" class="btn btn-primary">Save and Continue</button>
    <button type="reset" class="btn">Cancel</button> -->
    </div>
  </fieldset>

  <% end %>

This is the HTML it produced:

<form accept-charset="UTF-8" action="/listings" class="simple_form new_listing" id="new_listing" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="1oDfg41Wx/SiPCsvf4qTAcxhFUGSNlhlLfkMy8nHW1E=" /></div>

              <fieldset>
                <div class="control-group select optional controls"><label class="select optional control-label control-label" for="listing_listing_category_id">Category:</label><div class="controls"><select class="select optional" id="listing_listing_category_id" name="listing[listing_category_id]"><option value=""></option>
<option value="1">Exclusive</option>
<option value="2">Open</option></select></div></div>

          <div style="display: none;" data-show-for="Exclusive">
            <div class="control-group select optional form-horizontal"><label class="select optional control-label" for="listing_listing_type_id">Listing type</label><div class="controls"><select class="select optional" id="listing_listing_type_id" name="listing[listing_type_id]"><option value=""></option>
</select></div></div>
            <div class="control-group select optional form-horizontal"><label class="select optional control-label" for="listing_user_id">User</label><div class="controls"><select class="select optional" id="listing_user_id" name="listing[user_id]"><option value=""></option>
<option value="1">First User</option>
<option value="2">Second User</option>
<option value="3">Third User</option>
<option value="4">Fourth User</option>
<option value="5">Fifth User</option></select></div></div>
            <div class="control-group select optional form-horizontal"><label class="select optional control-label" for="listing_boro_id">Boro</label><div class="controls"><select class="select optional" id="listing_boro_id" name="listing[boro_id]"><option value=""></option>
<option value="1">Brooklyn</option></select></div></div>
          </div>

          <div style="display: none;" data-show-for="Open">
            <div class="control-group select optional form-horizontal"><label class="select optional control-label" for="listing_neighborhood_id">Neighborhood</label><div class="controls"><select class="select optional" id="listing_neighborhood_id" name="listing[neighborhood_id]"><option value=""></option>
<option value="1">Prospect Heights</option></select></div></div>    
            <div class="control-group select optional form-horizontal"><label class="select optional control-label" for="listing_building_id">Building</label><div class="controls"><select class="select optional" id="listing_building_id" name="listing[building_id]"><option value=""></option>
<option value="1">Trump Towers</option></select></div></div>
            <div class="control-group select optional form-horizontal"><label class="select optional control-label" for="listing_term_id">Term</label><div class="controls"><select class="select optional" id="listing_term_id" name="listing[term_id]"><option value=""></option>
</select></div></div>
            <div class="control-group select optional form-horizontal"><label class="select optional control-label" for="listing_property_type_id">Property type</label><div class="controls"><select class="select optional" id="listing_property_type_id" name="listing[property_type_id]"><option value=""></option>
<option value="1">Coop</option></select></div></div>
          </div>


    <div class="form-actions">
                <input class="btn btn btn-primary" name="commit" type="submit" value="Create Listing" />
          <!-- <button type="submit" class="btn btn-primary">Save and Continue</button>
          <button type="reset" class="btn">Cancel</button> -->
                </div>
              </fieldset>

</form>

This is the JS that I included in my listing.js – which corresponds to the file that this form is on app/views/listings/new.html.erb

$(document).ready(function({

    $('#listing_listing_category_id').on('change', function(){
        var option = $(this).find(':selected');
        $('[data-show-for]').hide(); //And maybe reset?
        $('[data-show-for="+ option.text +"]').show()
    });

});

When I chose the option I want, it doesn’t show me the fields I want to see.

  • 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-17T19:32:09+00:00Added an answer on June 17, 2026 at 7:32 pm

    Personally I wouldn’t use AJAX, just straight JS/JQuery to show/hide on click using data attributes.

    See fiddle: http://jsfiddle.net/XnPZF/

    First, add the data attributes to the sections you want to hide/show:

    <div style="display: none;" data-show-for="coop">
      <%= f.input :maintenance %>
      <%= f.input :coop_deductible %>
      <%= f.input :flip_tax %>
    </div>
    
    <div style="display: none;" data-show-for="condo">
      <%= f.input :common_charges %>
      <%= f.input :taxes %>
    </div>
    

    Then create a change event on the select:

    $('#listing_property_type').on('change', function(){
        var option = $(this).find(':selected');
        $('[data-show-for]').hide(); //And maybe reset?
        $('[data-show-for='+ option.text() +']').show()
    });
    

    For data-show-for, you can use Option text or value, just be sure to make sure the event knows which. If you plan to use this a lot of times, you could generalize it, but that would mean building your options.

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

Sidebar

Related Questions

My overall use case: I have a Listing model that has many images .
I have a model that has amenities with a 1:1 relationship: class Listing <
I have model Article it has field title with some text that may contain
I have a complex form for a model Schedule that belongs to a parent
I have a circular dependency in my Django models, such that model A has
I have a form where the model has a nested resource (an image uploaded
I have a Rails simple application that has two main models. A person model
I have the following code to generate a table listing brand/model/submodel/style for a group
I have model with a location, which itself has a latitude and longitude. What
I have model Foo which has field bar. The bar field should be unique,

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.