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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:32:53+00:00 2026-06-12T00:32:53+00:00

My goal is to use the nested_form gem: https://github.com/ryanb/nested_form but instead of creating just

  • 0

My goal is to use the nested_form gem: https://github.com/ryanb/nested_form
but instead of creating just a new set of labels and fields every time you add an object, I wanted to insert a row into an existing table.

= nested_form_for @transaction do |f|
%h3 Line Items 
%table
  %tr
    %th Branch
    %th Department
    %th Invoice #
    %th Amount
    %th Transaction Type
    %th Deposit (y/n)
    %th

  = f.fields_for :line_items do |line_item|
    %tr
      %td 
        = line_item.text_field :location_id
      %td 
        = line_item.text_field :department_id
      %td 
        = line_item.text_field :invoice_num
      %td 
        = line_item.text_field :amount
      %td 
        = line_item.text_field :transaction_type
      %td 
        = line_item.text_field :deposit
      %td= line_item.link_to_remove "Remove"
    %p= f.link_to_add "Add", :line_items

The .link_to_add button just creates a bunch of fields in the first row, first td.

<h3>Line Items</h3>
<table>
  <tr>
    <th>Branch</th>
    <th>Department</th>
    <th>Invoice #</th>
    <th>Amount</th>
    <th>Transaction Type</th>
    <th>Deposit (y/n)</th>
    <th></th>
  </tr>
  <div class="fields"><tr>
    <td>
      <input id="transaction_line_items_attributes_0_location_id" name="transaction[line_items_attributes][0][location_id]" size="30" type="text" />
    </td>
    <td>
      <input id="transaction_line_items_attributes_0_department_id" name="transaction[line_items_attributes][0][department_id]" size="30" type="text" />
    </td>
    <td>
      <input id="transaction_line_items_attributes_0_invoice_num" name="transaction[line_items_attributes][0][invoice_num]" size="30" type="text" />
    </td>
    <td>
      <input id="transaction_line_items_attributes_0_amount" name="transaction[line_items_attributes][0][amount]" size="30" type="text" />
    </td>
    <td>
      <input id="transaction_line_items_attributes_0_transaction_type" name="transaction[line_items_attributes][0][transaction_type]" size="30" type="text" />
    </td>
    <td>
      <input id="transaction_line_items_attributes_0_deposit" name="transaction[line_items_attributes][0][deposit]" size="30" type="text" />
    </td>
    <td><input id="transaction_line_items_attributes_0__destroy" name="transaction[line_items_attributes][0][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields" data-association="line_items">Remove</a></td>
  </tr>
  <td><a href="javascript:void(0)" class="add_nested_fields" data-association="line_items">Add</a></td>
  </div>
</table>

I’ve tried placing the .link_to_add in a few places, but it doesn’t put them in their own row.

Is there an easy way to go about adding a row of input boxes each time?

  • 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-12T00:32:55+00:00Added an answer on June 12, 2026 at 12:32 am

    This helped me a lot: https://github.com/ryanb/nested_form/wiki/How-To:-Render-nested-fields-inside-a-table

    By default fields_for inside nested_form_for adds <div class="fields"> wrapper around every nested object. But when you need to render nested fields inside a table you can disable default wrapper using :wrapper => false option and use the custom one:

    <table>
      <%= f.fields_for :tasks, :wrapper => false do |task_form| %>
        <tr class="fields">
          <td>
            <%= task_form.hidden_field :id %>
            <%= task_form.text_field :name %>
          </td>
          <td><%= task_form.link_to_remove 'Remove' %></td>
        </tr>
      <% end %>
      <tr>
        <td><%= f.link_to_add 'Add', :tasks %></td>
      </tr>
    </table>
    

    Note: You need to specify id field. Otherwise fields_for will insert it after </tr>.

    Also you need to override default behavior of inserting new subforms into your form using javascript:

    window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) {
      var $tr = $(link).closest('tr');
      return $(content).insertBefore($tr);
    }
    

    A similar technique can be used for lists, for compatibility with a jQuery UI sortable list.

    If you are using simple_form then add the :wrapper => false option to the surrounding simple_nested_form_for call, otherwise it gets overwritten by the :wrapper => nil default.

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

Sidebar

Related Questions

The project has a goal: use interface centric design. Basically we declare classes of
Goal To use a CREATE TYPE statement in HSQLDB 2.0.0 to create a user-defined
My goal is to use the result of an MD5 result to index a
My goal is to use a script that will install an executable file on
I've recently compiled Clang and LLVM on Windows. My goal is to use it
I'm trying to use custom meta boxes in wordpress. My goal at the moment
My ultimate goal is to load controls as plugins, for use as DocumentContent in
Okay, so my goal is to build a easy to use protocol for sending
Goal is to make a dialog that appears on menu_key pressed, but it keeps
I originally intended to use MATLAB to tackle this problem but the in-built function

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.