Form with saved and new model instances

Hello all - I’m looking to create a form that would allow the editing
of existing model instances while also allowing new model instances to
be created. (its an order form where the model instances would be an
order line item joining the order to an item)
It looks like either one alone is easy, because in the former case the
field names would be foo[id] where the id is from the database, and in
the latter case, where the models are all new, it would be foo[index].
I’d like to add the new “empty” rows using some javascript which
should be easy, I just don’t know how rails will approach the unsaved
rows in this case.
How does one mix these models? In my case, I can’t create new “empty”
models for the new rows, because of some not null constraints in the
database.
How do other folks deal with situations like this?

Thanks for any advice or comments
-James

How does one mix these models? In my case, I can’t create new “empty”
models for the new rows, because of some not null constraints in the
database.

Actually you can create a new “dummy” Model instance and pass it to the
view. Because it has a bunch of blank fields in it, you cannot save it,
but it can be used to create the form in the view. The "dummy object
will disappear as soon as the view is finished rendering never having
seen the database.

When the user gets done filling in the form, they will hopefully have
provided you with all the data you need to create another new instance
of your model. This time you will be able to save it to the database
(or complain to the user about missing data)

The real trick here is distinguishing between a Form that is returning a
new object and a form returning an updated existing object. There are a
number of ways to do this and I don’t like any of them a lot so if you
need help with this piece, asks someone with a stronger opinion.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs