Sterling Rose Design Blog
Dynamic form with a many-to-many relationship
Authored by Dana Jones
May 30, 2009 00:57
1 Comments
Tags: Javascript Rails associations
Authored by Dana Jones
May 30, 2009 00:57
1 Comments
Tags: Javascript Rails associations
My customer’s application is an in-house ordering system (among other things). The order form has a drop-down list to select the client placing the order. It also has two other drop-down lists: primary_contact_id and secondary_contact_id, both of which are based on the Contact model. My customer is certain he will only ever need to associate two contacts with an order, so I’m comfortable with this approach.
The sticking point was that I only want to populate the primary and secondary contact select boxes with contacts that are associated with a particular client. Client and Contact have a many-to-many association.
Ryan Bates’ Dynamic Select Menus Railscast got me most of the way there, but his example (countries/states) was one-to-many, not many-to-many. Following was my adaptation:
Read the whole post...The sticking point was that I only want to populate the primary and secondary contact select boxes with contacts that are associated with a particular client. Client and Contact have a many-to-many association.
Ryan Bates’ Dynamic Select Menus Railscast got me most of the way there, but his example (countries/states) was one-to-many, not many-to-many. Following was my adaptation:
views/javascripts/dynamic_contacts.js.erb
var contacts =...
Calculating Line Item Extensions
Authored by Dana Jones
May 25, 2009 14:48
0 Comments
Tags: Javascript Rails Prototype AJAX
Authored by Dana Jones
May 25, 2009 14:48
0 Comments
Tags: Javascript Rails Prototype AJAX
In my project, I have orders, and each order can have an unlimited number of line_items. Line_items are created by the user clicking on a button, which appends (via RJS) a new row to the line_items tabled form. So far, so good.
But I needed the extended price (quantity * price_per) of each line_item to be calculated every time the user tabbed out or clicked away from the price_per field. Further, I needed the subtotal, tax, total, and balance fields to be automatically re-calculated.
I messed around with it for several hours, trying Javascript, Prototype, and even jQuery, before I finally settled on a Prototype approach that worked. The real struggle was that using Rails 2.3’s nested forms functionality meant that each line_item would have an index key embedded in the middle of the text field’s name and id, and I could not come up with a good way to extract it, to pass it to the Javascript function.
Luckily...
Read the whole post...But I needed the extended price (quantity * price_per) of each line_item to be calculated every time the user tabbed out or clicked away from the price_per field. Further, I needed the subtotal, tax, total, and balance fields to be automatically re-calculated.
I messed around with it for several hours, trying Javascript, Prototype, and even jQuery, before I finally settled on a Prototype approach that worked. The real struggle was that using Rails 2.3’s nested forms functionality meant that each line_item would have an index key embedded in the middle of the text field’s name and id, and I could not come up with a good way to extract it, to pass it to the Javascript function.
Luckily...

