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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:55:35+00:00 2026-06-07T03:55:35+00:00

So in a rails app I have a table in my database, hard drives,

  • 0

So in a rails app I have a table in my database, hard drives, and for each hard drive I have a variable number of heads and platters. When I create or edit the hard drive, I want to either a) display dynamically the number of head status and platter status forms that are needed or b) route the user to a page that has the appropriate number of head and status forms continuously on the page. I favor option a, but I think it will require some custom javascript or something in order for it to work (not really feasible unless there is a plugin, I don’t know javascript). Really, I want to reject any hard drive that not have it’s platter and head statuses entered.

(variable names are of course hard_drive, platter_status, and head_status)

For an example, if I have a laptop drive, I want to define that it has 1 head and platter while creating the drive. Either when the value in the combobox for the head & platter count changes, display the form for the platter & head status. When the create button is hit, it should use the data to create the hard_drive in the database, and each of the platter_status and head_status items as appropriate.

How do I do this? I know that in the view, I have to add something that will detect the change in the combo box and display the layout using the value from the combobox.

It is similar in nature to this


Edit 1:
So, I have this, and so far it’s not working, but I have the right idea. I stumbled upon some threads that talk about using javascript to dynamically add html.

  <div class="field" id="head_count" >
    <label for="hard_drive_head_count">Head count</label><br />
    <input id="hard_drive_head_count" name="hard_drive[head_count]" type="number" value="0" />
    <script type="text/javascript">
      var input = document.getElementById('hard_drive_head_count'),
        events = [
        "input",
         ];
      events.map(function(ev) {
      input.addEventListener(ev, function() {
        console.log(ev + ' => ' + input.value);
    if (parseInt(input.value) === 1) {
      document.getElementById('platter_statuses').innerHTML= "howdy";
    } else {
      document.getElementById('platter_statuses').innerHTML="";
    }
      }, false);
      });
    </script>
  </div>
  <label for="platter_status">Platter status</label><br />
  <div class="field" id="platter_statuses" >

  </div>

Now that I have this however, the question moves to – how can I populate this quickly with the forms from the _form.html.erb for the platter_status views, with the stuff all filled in from the database as it should?
I thought about in the else, using some dynamic ruby, but I don’t know exactly how to pass an argument (the platter number and the drive id) so that the database knows what to populate it with, and I don’t know what to put after the @ in <%= @SOMETHING_HERE %> so that it resolves to the platter_status view…

  • 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-07T03:55:38+00:00Added an answer on June 7, 2026 at 3:55 am

    I actually found that there is a helper that is great for what I needed. It’s the accepts_nested_attributes_for function in the model. This, combined with rails cast 196 and 197 helped me achieve exactly what I wanted.

    In the hard_drive model, you should have a:

    attr_accessible :head_statuses_attributes
    has_many :head_statuses, :dependent => :destroy
    accepts_nested_attributes_for :head_statuses, :allow_destroy => true
    

    for the head_status and platter_status models.

    In the view, you’ll need a line like this:

    <%= link_to_add_fields "add head status", f, :head_status %>
    

    and in the partial outside of any tag, you’ll need a:

     <%= link_to_remove_fields "Remove", f %> 
    

    For more information, see these articles:
    how accepts_nested_attributes_for works

    How the dynamic editing is possible
    How the dynamic editing is possible part 2

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

Sidebar

Related Questions

I have a Rails app using a Postgres database with a table called geolite_blocks.
I have a Ruby/Rails app. I have an artists table in my postgresql database
Hi I have a table in my rails app that doesnt have any id
I have a Rails app that processes a large (millions) number of records in
I have a rails app that requires multi-table inheritance for which I am using
I have to connect my rails app in a legacy Postgre database. It uses
I have a rails app with a database of listings:addresses with coordinates in it.
Simple rails app: I have 2 models, user and intro [which is simply a
In my rails app I have user, school, and course models. I have set
In a Rails app I have Sales and Salespeople: class Sale < ActiveRecord::Base belongs_to

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.