views:

149

answers:

2

Hello, I am using Ruby on Rails and need to create a view that allows the creation of records through a HABTM relationship to another model. Specifically, I have the following models: Customer and ServiceOverride, and a join table customers_serviceoverrides. Using the customer view for create/update, I need to be able to create, update and delete ServiceOverrides and manage the attributes of the associated model(s) from the same view.

Visually I'd prefer to have something like a plus/minus sign to add/delete service overrides, and each serviceoverride record has two string entities which need to be displayed and editable as well. However, if I could just get the code (a kind of nested form, I'm assuming?) working, I could work out the UI aspects.

The models are pretty simple:

class ServiceOverride < ActiveRecord::Base
    has_and_belongs_to_many :customers
end

class Customer < ActiveRecord::Base
    has_and_belongs_to_many :serviceoverrides
end

The closest thing I've found explaining this online is on this blog but it doesn't really address what I'm trying to do (both manage the linkages to the other model, and edit attributes of that model.

Any help is appreciated. Thanks in advance.

Chris

A: 

For each customer:

  • A list with the existing overrides:
    • Override | a | b [edit]
    • Override | x | z [edit]
    • ...
    • [Add an ovverride]

[edit] just replaces an ovverride with an edit form:

  • Override | a_ | b_ [save]

[Add an override] appends the form to the same form to the list.

Submit forms via ajax. For scripting I recommend jQuery as opposed to Prototype.

glebm
+1  A: 

The ascii cast at http://asciicasts.com/episodes/17-habtm-checkboxes has a simple and functional example.