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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:11:08+00:00 2026-05-16T16:11:08+00:00

Got a rails application that has a form (new action) with a select with

  • 0

Got a rails application that has a form (new action) with a select with an options value range from 1 to 5. When the users selects one of the options I need to use ruby “build” to build a number of objects depending on what the select value is.

So when the user selects 2 in the select box the following code needs to run:

@rooms = 2.times { @travel.rooms.build }

Also when the users changes this value from 2 to 1 the code needs to be replaced with

@rooms = 1.times { @travel.rooms.build }

Basically I’m having trouble finding a way to setting the value in jQuery and then updating it in the html. Getting the value is no problem, it’s how to dynamically change it in the html that’s the issue.

Any tips on how to proceed with this?

Thanks,
Amund

Edit – Clarification

I need this to happen before the form is submitted.

@rooms = 2.times { @travel.rooms.build }

So any changes to the above needs to happen while the user is on the form page.

  • 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-05-16T16:11:09+00:00Added an answer on May 16, 2026 at 4:11 pm

    I’ve found a way that works, although it’s not pretty nor does it work if the user doesn’t have JS (In my case if the user don’t have JS they can gtfo).

    Below is the code needed for it to work. (Keep in mind this works, but I’ve done nothing to make it more concise or better). ps. It looks like hell.

    $('#room_details .fields').hide();
    $('#room_details .fields:eq(0)').show();
    $('.fields:eq(0)').find('.is_wanted').val("true");
    $('#number_of_rooms').change(function() {
    $('#room_details .fields').hide();
    $('.is_wanted').val("");
    if($(this).val() == '1') {
      $(".fields:eq(0)").each(function() {
        $(this).show();
        $(this).find('.is_wanted').val("true");
      });
    }
    else if ($(this).val() == '2'){
      $(".fields:eq(0), .fields:eq(1)").each(function() {
        $(this).show();
        $(this).find('.is_wanted').val("true");
      });
    }
    else if ($(this).val() == '3'){
      $(".fields:eq(0), .fields:eq(1), .fields:eq(2)").each(function() {
        $(this).show();
        $(this).find('.is_wanted').val("true");
      });
    }
    else if ($(this).val() == '4'){
      $(".fields:eq(0), .fields:eq(1), .fields:eq(2), .fields:eq(3)").each(function() {
        $(this).show();
        $(this).find('.is_wanted').val("true");
      });
    }
    else if ($(this).val() == '5'){
      $(".fields:eq(0), .fields:eq(1), .fields:eq(2), .fields:eq(3), .fields:eq(4)").each(function() {
        $(this).show();
        $(this).find('.is_wanted').val("true");
      });
    }
    });
    

    Then in the model we do:

    accepts_nested_attributes_for :rooms, :allow_destroy => true, :reject_if => proc { |attrs| attrs['is_wanted'].blank? }
    

    Where is_wanted is a hidden field in the form for each room. I’m sure there are prettier ways, but at least it works.

    Any thoughts?

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

Sidebar

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.