Nested form and a link to add fields question?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rubyguage48
    New Member
    • Aug 2012
    • 1

    Nested form and a link to add fields question?

    I seem to be stuck on a very steep learning curve in my quest to learn rails ... essentially from tutorials and the like on the web ... my profession is accounting so please excuse the lack of sophistication in my attempt to speak rails or for that matter code it.

    I subscribed to Ryan Bate' web site and got much of the idea and knowledge on how to create this nested form from his screencast on Deeply Nested Forms. The code "Def of link_to_add_fie lds" is from his site and is to be found at the end of this posting.

    My Add Contract nested form is as follows, new.html.haml:

    Code:
    - provide(:title, 'Add Contract')
                %h2 New Contract
                = form_for(@codeline) do |codeline|
                  =render 'shared/codeline_error_messages', object:
                  codeline.object
                  =render 'fields', codeline:  codeline
                  /= link_to_add_fields "Add Codes", codeline, :code
                  /debugger
                  .actions
                     = codeline.submit "Save", class: 'save strong
                     round'
    I am not sure as to where to add the link_to_add_fie lds code?

    The _fields partial:

    Code:
    <fieldset><legend>Enter Contract Details</legend>
              = codeline.fields_for :contract do |contract|
              .field
                 = contract.label :name, "AuthNum"
                 %br/
                 = contract.text_field  :authnum, :size => 10, :class
                 => "ui-state-default"
               .
               .
               .
               </fieldset>
               <fieldset><legend>Enter Client Details</legend>
               = codeline.fields_for :clients do |client|
               .field
                  = client.label :name, "First Name"
                  %br/
                  = client.text_field :f_name, :size => 15, :class =>
                  "ui-state-default"
                .
                .
                .
                </fieldset>
                <fieldset><legend>Enter Billing Code Details</legend>
                = codeline.fields_for :codes do |code|
                .field
                   = code.label :name, "Code Name"
                   %br/
                   = code.text_field :code_name, :size => 15, :class
                   => "ui-state-default"
                 .field
                   = code.label :name, "Status"
                   %br/
                   = code.text_field :status, :size => 10, :class => 
                   "ui-state-default"
                 .field
                    = code.label :name, "Description"
                    %br/
                    = code.text_field :description, :size => 25,
                    :class => "ui-state-default"
                  .field
                    = codeline.label :name, "Units Alloc"
                    %br/
                    = codeline.text_field :units_alloc, :precision 
                    => 6, :scale => 2, :size => 10, :class =>
                   "ui-state-default"
                 </fieldset>
    My intent is to be able to use the link_to_add_fie lds code so that I am able to add a complete row of these last four attributes, "Code Name", "Status", "Descriptio n", and "Units Alloc", to my contract form. The reasoning is that some contracts only have one Code authorized but other will have more and each contract will differ. You will also notice that the whole nested form is centered on the join table 'Codelines'. I read somewhere on the web,
    Code:
    https://makandracards.com/makandra/1346-debug-nested-forms
    and I could dig up the reference if you are interested(I thought I would add the reference now), that if you have an extra attribute in your join table then you have to base your nested form on the join table. My extra attribute is "Units Alloc" and the reason for this is that many contracts will have different constellations of codes(read billing codes) authorized, in many instances they might be using the same codes but with different amounts of "Units Alloc" and that is why I needed the Units Alloc in my join table as an extra attribute.

    The application helper follows:

    Code:
    module ApplicationHelper
    
           def logo
             logo = image_tag("rails.png", alt: "Time and it's Cost", 
             class:  "round")
           end
    
           # Return title on a per-page basis.
           def full_title(page_title)
             base_title = "Time and it's Cost"
             if page_title.empty?
                base_title
             else
                "#{base_title} | #{page_title}"
             end
           end
    
          def link_to_add_fields(add_fields, codeline, code)
           /debugger/
           new_object = codeline.object.send(code).klass.new
           id = new_object.object_id
             fields = codeline.fields_for(code, new_object,
             child_index: id) do |builder|
              render(code.to_s.singularize + "_fields", f: builder)
             end 
           link_to(name, '#', class: "add_codes", data: {id: id,
           fields: fields.gsub("\n", "")})
           end
          end
    I do not understand the def link_to_add_fie lds code sufficiently enough to adapt it such that I am able add a complete row of four attributes, three from one table and the fourth from the join table.

    Hopefully one of you folks can see your way to helping me understand how to make this part of the nested form work.

    As you can see from the attachment the form works just fine without the link_to_add_fie lds and it also saves to the appropriate tables.

    Any help or suggestions would be appreciated.

    Thanks.
    Last edited by rubyguage48; Aug 14 '12, 08:34 PM. Reason: Added the reference to debug-nested-forms. I changed = contract.fields for to codeline.fields for and I removed the contracts.codelines field for line ... it was an error.
Working...