How can I save values before the record is created?

I've been doing a lot of work with pop up windows and have now come to the "it all works, but this" point in my application.

I have an order application. With that I have a pop up window that allows users to do some dragging and dropping of items for a portion of the order. The problem is that the pop up uses the order id. The items selected in the pop up window are saved back to the order record as a user drags and drops items. Obviously works great with existing orders, but blows up with new orders.

I am saving everything back to the order record. There is not a separate table to hold the list for this item of the order.

Right now the I open the pop-up window using link_to and sending in the id of the order. If it's a new order, I get "Couldn't find Order without an ID".

Since the Order ID is integral to everything working, what are my options? Do I check for the order ID and if it's not provided, start a new order and save it to the database?

Or do I add a route for the model/pop_up?

Right now my route is map.resources :model, :collection => ["pop_up"]

Or is this a bigger, uglier problem that really can't be attacked with this design? If that's the case, some ideas for how to fix, without starting all over would be appreciated.

That sounds like it would work.

Alternatively, create a new order when the page that launches the popup is called (or somewhere earlier in the process, like when an item is first added to the cart).

HTH,

Right now the I open the pop-up window using link_to and sending in the id of the order. If it's a new order, I get "Couldn't find Order without an ID".

Since the Order ID is integral to everything working, what are my options? Do I check for the order ID and if it's not provided, start a new order and save it to the database?

That's an option. Or I'd just create it arbitrarily before the pop up. Save grief :slight_smile: Having a few extras around isn't a problem.

Roger Pack wrote: Or I'd just create it arbitrarily before the pop

up. Save grief :slight_smile:

As always, thanks for the great ideas.

The pop-up is opened with link_to. When would the order be created? I don't think that I really want to create the order when the new order window is opened, so I think that I'd want to create it after that.

My thought is to put this in the controller that associated with the pop-up window opening and it's params.

Also, I know this is super basic, but could someone point me to some syntax that would create the new order and then grab the ID after the new order is saved in the db. Order.create just creates a nil value for ID.

From an outsider's perspective it sounds like you have two different but similar things going on: - Order#edit with drag-and-drop to add and remove items from an existing order - Order#new with drag-and-drop to set the initial list of items on the order

The fact that these two distinct actions have very, very similar UIs is just clouding what's going on.

With that in mind I can think of two different approaches: 1. Have distinct new and edit views that pull in the drag-and-drop functionality via partials. 2. Have one popup view but pass in the url and http verb for the form postback. Here you'd give the appropriate urls for create or update.

Yes. You could put a hidden field on the form that maps to the attribute where you store the string of ids. When you declare the droppable, add an onDrop function that pushes the id of the item onto the end of the string. I'm not sure what you'd hook for the remove (sure wish they'd fix the script.aculo.us wiki...).

Good luck!