Sven Magnus wrote:
So, is this approach any good?
As Russel notes, you need to recognize that some users may not have js enabled and deal with that somehow. How you do it really depends on your business model. More on that below.
- Store the new "item" in a session variable
It's early here and I don't have a threaded email reader so I don't know if there was something in your previous post that would drive storing the item in a session variable. The choice between storing items in session variables and in the database depends on too many things to give advice on without more info.
- Create two diffferent forms on the same page, one for adding the
item and one for adding the subitems using Ajax
- The subitem form will add new subitems to the item stored in the
Is that how this kind of thing should/coul be done?
The general model would be to first render the main form for adding items. That form could have an element on it that would trigger the rendering of the second form. So maybe there's a checkbox labeled 'add sub-items' next to an item on the main form. You put an observe_field on that checkbox. In your design thinking, you have to figure out how you're going to handle the transaction in both js-enabled and js-disabled states. Here's one, still-working-on-my-first-cup-of-java approach.
If the user has js enabled, when they check the checkbox it will trigger a method in the controller which uses RJS to render the sub-form immediately. So if they've got js-enabled they'll go ahead and add sub-items before submitting the main form. If the user doesn't have js enabled, the observer won't do anything. When the main form is submitted, you check the value of the checkbox. If it's checked, you check to see if you have any sub-items. If you do, you assume they have js-enabled, that they're done with that, and take them to the next step in the process. If you don't, you assume they have js-disabled and take them to an interim page where they can enter the sub-items.
The respond_to method can let you know very early in the user's visit whether or not they have js-enabled. This gives you the ability to plan entirely different 'routes' through the app. Or you can take an approach like the above. Or you can, depending on your business model, decide not to allow the use of the site by js-disabled browsers. All, and more, are valid choices. It's important to make that decision explicitly so you can communicate it if needed to your visitors.