How to get current time including milliseconds for div id?

James West wrote:

I need to auto generate a guaranteed to be unique number or string to be used something like this in a partial view <div id=<%=unique_div_id%>>

First of all: what's the purpose of this? Why do you need all these unique IDs? I want to make sure you've got an appropriate solution here. As far as your question, why not just use GUIDs?

Best,

Marnen Laibow-Koser wrote:

James West wrote:

I need to auto generate a guaranteed to be unique number or string to be used something like this in a partial view <div id=<%=unique_div_id%>>

First of all: what's the purpose of this? Why do you need all these unique IDs? I want to make sure you've got an appropriate solution here. As far as your question, why not just use GUIDs?

Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org

GUIDS may well be the solution I need :slight_smile:

Any idea howe to create them :slight_smile:

See my earlier response for the reason why I need them. And thank you for taking so much trouble to understand my problem

[...]

GUIDS may well be the solution I need :slight_smile:

Any idea howe to create them :slight_smile:

Um, yeah...use the uuid gem. (Note: I've never done this, but I'd be surprised if it doesn't work.) See http://github.com/assaf/uuid/tree/master .

Or you could just increment a counter as you create fields, so that you have a serial number to put in your id attributes.

See my earlier response for the reason why I need them.

Your earlier response suggests that maybe GUIDs are the wrong thing. If the objects are being dynamically created, then as long as Ruby knows about them, even if they haven't been saved to the DB, you can at least use the object_id which is guaranteed to be unique...

And thank you for taking so much trouble to understand my problem

You're welcome!

Best,

Marnen Laibow-Koser wrote:

[...]

Or you could just increment a counter as you create fields, so that you have a serial number to put in your id attributes.

Now that has possibilities. I don't know why I didn't think of that. Probably because I had my head stuck on stateleseness when in fact I'm not hitting the server at all so that should work and I think it's going to be the best solution.

Your earlier response suggests that maybe GUIDs are the wrong thing.

I agree. It seems like overkill when simpler solutions should be available but I'm grasping at straws.

If the objects are being dynamically created, then as long as Ruby knows about them, even if they haven't been saved to the DB, you can at least use the object_id which is guaranteed to be unique...

Unfortuntely object_id is not unique. I changed the view to do this   <%-list_form_id = "list-form-#{f.object_id}"-%>   <%-edit_form_id = "edit-form-#{f.object_id}"-%> which is fine for existing records. It's also fine for the first new record but subsequent new records show the same object_id as the first new record.

This is the result of inspecting the HTML source (firebug) on my test data with 3 new records added after making the above change.

You can see that the last 3 all have the same ID list-form-35646040

<ul> <input id="user_addresses_attributes_0_id" type="hidden" value="13" name="user[addresses_attributes][0][id]"/> <div id="list-form-35574920"> </div> <div id="edit-form-35574920" style="display: none;"> </div> <input id="user_addresses_attributes_1_id" type="hidden" value="17" name="user[addresses_attributes][1][id]"/> <div id="list-form-35566050"> </div> <div id="edit-form-35566050" style="display: none;"> </div> <input id="user_addresses_attributes_2_id" type="hidden" value="18" name="user[addresses_attributes][2][id]"/> <div id="list-form-35556290"> </div> <div id="edit-form-35556290" style="display: none;"> </div> <input id="user_addresses_attributes_3_id" type="hidden" value="19" name="user[addresses_attributes][3][id]"/> <div id="list-form-35545190"> </div> <div id="edit-form-35545190" style="display: none;"> </div> <input id="user_addresses_attributes_4_id" type="hidden" value="20" name="user[addresses_attributes][4][id]"/> <div id="list-form-35535650"> </div> <div id="edit-form-35535650" style="display: none;"> </div> <input id="user_addresses_attributes_5_id" type="hidden" value="21" name="user[addresses_attributes][5][id]"/> <div id="list-form-35524420"> </div> <div id="edit-form-35524420" style="display: none;"> </div> <input id="user_addresses_attributes_6_id" type="hidden" value="22" name="user[addresses_attributes][6][id]"/> <div id="list-form-35515770"> </div> <div id="edit-form-35515770" style="display: none;"> </div> <input id="user_addresses_attributes_7_id" type="hidden" value="23" name="user[addresses_attributes][7][id]"/> <div id="list-form-35506520"> </div> <div id="edit-form-35506520" style="display: none;"> </div> <input id="user_addresses_attributes_8_id" type="hidden" value="24" name="user[addresses_attributes][8][id]"/> <div id="list-form-35497950"> </div> <div id="edit-form-35497950" style="display: none;"> </div> <input id="user_addresses_attributes_9_id" type="hidden" value="25" name="user[addresses_attributes][9][id]"/> <div id="list-form-35489260"> </div> <div id="edit-form-35489260" style="display: none;"> </div> <input id="user_addresses_attributes_10_id" type="hidden" value="26" name="user[addresses_attributes][10][id]"/> <div id="list-form-35478670"> </div> <div id="edit-form-35478670" style="display: none;"> </div> <input id="user_addresses_attributes_11_id" type="hidden" value="31" name="user[addresses_attributes][11][id]"/> <div id="list-form-35470130"> </div> <div id="edit-form-35470130" style="display: none;"> </div> <div id="list-form-35646040"> <li> </li> </div> <div id="edit-form-35646040" style="display: none;"> </div> <div id="list-form-35646040"> </div> <div id="edit-form-35646040" style="display: none;"> </div> <div id="list-form-35646040"> </div> <div id="edit-form-35646040" style="display: none;"> </div> </ul>

Marnen Laibow-Koser wrote:

[...]

Or you could just increment a counter as you create fields, so that you have a serial number to put in your id attributes.

Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org

Problem solved

Partial now has this <%if f.object.new_record? %>   <%-list_form_id = "list-form-#{uid}"-%>   <%-edit_form_id = "edit-form-#{uid}"-%> <%-else-%>   <%-list_form_id = "list-form-#{f.object_id}"-%>   <%-edit_form_id = "edit-form-#{f.object_id}"-%> <%-end-%>

The helper now does this   def add_address_link(form_builder)     link_to_function 'New address' do |page|       form_builder.fields_for :addresses, Address.new, :child_index => 'NEW_RECORD' do |f|       html = render(:partial => 'address_form', :locals => { :f => f, :uid => 'NEW_RECORD' })         page << "jQuery('#address_list ul').append('#{escape_javascript(html)}'.replace(/NEW_RECORD/g, new Date().getTime()))"       end     end   end

So the div ID now matches the child_index which was the ideal solution that I set out to achieve and all is good.

3 $$£"!"£$ weeks I've been trying to figure out the best way to deal with this and it was so simple in the end.

Huge thanks to ALL that responded it all helped to kick my ideas in the right direction.