473,378 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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_fields" 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:

Expand|Select|Wrap|Line Numbers
  1. - provide(:title, 'Add Contract')
  2.             %h2 New Contract
  3.             = form_for(@codeline) do |codeline|
  4.               =render 'shared/codeline_error_messages', object:
  5.               codeline.object
  6.               =render 'fields', codeline:  codeline
  7.               /= link_to_add_fields "Add Codes", codeline, :code
  8.               /debugger
  9.               .actions
  10.                  = codeline.submit "Save", class: 'save strong
  11.                  round'
I am not sure as to where to add the link_to_add_fields code?

The _fields partial:

Expand|Select|Wrap|Line Numbers
  1. <fieldset><legend>Enter Contract Details</legend>
  2.           = codeline.fields_for :contract do |contract|
  3.           .field
  4.              = contract.label :name, "AuthNum"
  5.              %br/
  6.              = contract.text_field  :authnum, :size => 10, :class
  7.              => "ui-state-default"
  8.            .
  9.            .
  10.            .
  11.            </fieldset>
  12.            <fieldset><legend>Enter Client Details</legend>
  13.            = codeline.fields_for :clients do |client|
  14.            .field
  15.               = client.label :name, "First Name"
  16.               %br/
  17.               = client.text_field :f_name, :size => 15, :class =>
  18.               "ui-state-default"
  19.             .
  20.             .
  21.             .
  22.             </fieldset>
  23.             <fieldset><legend>Enter Billing Code Details</legend>
  24.             = codeline.fields_for :codes do |code|
  25.             .field
  26.                = code.label :name, "Code Name"
  27.                %br/
  28.                = code.text_field :code_name, :size => 15, :class
  29.                => "ui-state-default"
  30.              .field
  31.                = code.label :name, "Status"
  32.                %br/
  33.                = code.text_field :status, :size => 10, :class => 
  34.                "ui-state-default"
  35.              .field
  36.                 = code.label :name, "Description"
  37.                 %br/
  38.                 = code.text_field :description, :size => 25,
  39.                 :class => "ui-state-default"
  40.               .field
  41.                 = codeline.label :name, "Units Alloc"
  42.                 %br/
  43.                 = codeline.text_field :units_alloc, :precision 
  44.                 => 6, :scale => 2, :size => 10, :class =>
  45.                "ui-state-default"
  46.              </fieldset>
My intent is to be able to use the link_to_add_fields code so that I am able to add a complete row of these last four attributes, "Code Name", "Status", "Description", 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,
Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. module ApplicationHelper
  2.  
  3.        def logo
  4.          logo = image_tag("rails.png", alt: "Time and it's Cost", 
  5.          class:  "round")
  6.        end
  7.  
  8.        # Return title on a per-page basis.
  9.        def full_title(page_title)
  10.          base_title = "Time and it's Cost"
  11.          if page_title.empty?
  12.             base_title
  13.          else
  14.             "#{base_title} | #{page_title}"
  15.          end
  16.        end
  17.  
  18.       def link_to_add_fields(add_fields, codeline, code)
  19.        /debugger/
  20.        new_object = codeline.object.send(code).klass.new
  21.        id = new_object.object_id
  22.          fields = codeline.fields_for(code, new_object,
  23.          child_index: id) do |builder|
  24.           render(code.to_s.singularize + "_fields", f: builder)
  25.          end 
  26.        link_to(name, '#', class: "add_codes", data: {id: id,
  27.        fields: fields.gsub("\n", "")})
  28.        end
  29.       end 
I do not understand the def link_to_add_fields 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_fields and it also saves to the appropriate tables.

Any help or suggestions would be appreciated.

Thanks.
Aug 14 '12 #1
0 4322

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Angus Comber | last post by:
Hello I am working with an external perl script. I can submit something like this: <form target="mycart" action="https://www.website.com/cgi-bin/" method="post"> <input type="image"...
1
by: James | last post by:
I am creating a play area inspection and re-ordering system. I have an inspection form which contains various fields including a site number field. When the user clicks a button it jumps to an...
4
by: Andy Proctor | last post by:
I hope there is an answer out there.... I have a simple database structured like this (non relevant tables and fields omitted) Members table memberID memberFname memberLname memberNokID
0
by: Ace Calhoon | last post by:
Hello, Using Access 2003 SP-1. I have a form which contains a subform. Each form represents a table, and is linked by two fields. These fields are also related in the underlying tables, with...
12
by: TheOne | last post by:
In Asp.net web form under form tag there is action field that I am point to some other page, and not to same web form. When I run this page it is always pointing to itself. How do I get around...
1
by: Woody Splawn | last post by:
I have a number of fields on a form in a certain solution. In another solution with a similar form I need to have these same fields. Do I need to create them again from scratch or will it work ok...
3
by: gn | last post by:
I have a simple database, I created a form using the form wizard to input data, however when I open up the form it is sorting all the records by the wrong field, how where do I change that? By...
1
by: mr k | last post by:
Hi, I wanted to use mail merge with forms but Text form fields are not retained during mail merge in Word, I got the code from Microsoft but it doesn't remember the text form field options such as...
1
by: apstein | last post by:
Hi, I have an HTML form (generated by php) which I am rendering in Internet Explorer 6. My original problem was that if the user submitted the form, and then used the back button to return to...
2
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi All! I am with a situation where I am not getting the right updating to the form's fields. The situation is the following one: I have one combobox and one textbox. I am using the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.