RJS form validation

Hi.

I'm trying to add and validate objects using RJS.

I have a page where if a user clicks on a link a form partial is inserted via javascript(depending on the click).

If that form is valid I would like to remove it and display the object, otherwise rewrite the form with the errors inline(next to each field).

I'm stuck at the point where I don't know how to rewrite the form. On the first insert the div with the form has no unique id(since a user can click multiple times and insert multiple forms, and I need the update the proper one).

My stupid idea is to generate a random number and pass that as an id to the div form and via hidden_field so I know what to remove/update.

I'm sure there has to be a better way. I've been looking for tutorials but I haven't found something specifically for what I want to do.