Forum: Ruby on Rails AJAX sub-list

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4904de38aabb5e2052f6ee5f153b7472?d=identicon&s=25 Kevin Davis (Guest)
on 2006-02-21 07:12
(Received via mailing list)
In the new / edit screen for an "item" I'd like to be able to add/remove
"sub-items"

on pressing an add button below the "sub-items" a bit of a form shows up
with the html looking something like:

<div id="uniqueid">
<!-- label etc here -->
<input type="text" id="sub_items[]" name="sub_items[]" size="20"/>
<a href="#" onclick="new Ajax.Updater('uniqueid', '/items/hide',
{asynchronous:true, evalScripts:true}); return false;" >delete</a>
</div>

The problem: making unique ids for these form elements, generate a
random
number and hope for no collisions? store it somewhere? (this led me to
all
kinds of questions about sessions)

Also: right now I've just got a action 'hide' that renders nothing that
I
use to delete the form element.. is there a better practice?

Basically I'm wondering how they did the lists in Backpack ;-)

Thanks!
675475d0b65710be6d992eb5eb2c61c2?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-02-21 13:32
(Received via mailing list)
On Tue, Feb 21, 2006 at 01:11:22AM -0500, Kevin Davis wrote:
} In the new / edit screen for an "item" I'd like to be able to
add/remove
} "sub-items"
}
} on pressing an add button below the "sub-items" a bit of a form shows
up
} with the html looking something like:
}
} <div id="uniqueid">
} <!-- label etc here -->
} <input type="text" id="sub_items[]" name="sub_items[]" size="20"/>
} <a href="#" onclick="new Ajax.Updater('uniqueid', '/items/hide',
} {asynchronous:true, evalScripts:true}); return false;" >delete</a>
} </div>

I recommend against using AJAX for this sort of thing. Yes, you want
JavaScript and DHTML, but not AJAX. The difference is that when adding
and
removing items, the user is likely to be more comfortable not having the
changes committed until s/he hits the Update (or Submit, or whatever)
button. If you use AJAX you are effectively committing every add and
remove
immediately.

} The problem: making unique ids for these form elements, generate a
random
} number and hope for no collisions? store it somewhere? (this led me to
all
} kinds of questions about sessions)

Use positive numbers for existing ids (probably already the case) and
negative numbers for newly created, and thus not yet committed, ids. I'm
assuming these subitems are a has_many relationship (though it would
work
just as well with habtm). In your controller, remove any missing
positive
ids and create subitems for any negative ids.

} Also: right now I've just got a action 'hide' that renders nothing
that I
} use to delete the form element.. is there a better practice?

Yes. Use JavaScript, not AJAX.

} Basically I'm wondering how they did the lists in Backpack ;-)

I have no idea. I'm talking about the right way to interact with a user.

} Thanks!
} Kevin
--Greg
This topic is locked and can not be replied to.