How to get current time including milliseconds for div id?

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%>>

I thought of using time.now but that only goes to seconds. I have tried
rand(time.now) but that does not guarantee a nique ID.
There would be more than one partial created within any given second
hence the need for time in milliseconds.
It doesn’t have to be a time function it can be anything so long as it
is fast, minimal memory footprint and can be stored in a variable for
use with a link_to_function that will toggle the visibility of the div.

I thought that this would be such a simple thing to do but I have been
tearing my hair out over this for days and I need a solution real fast.

Any ideas would be greatly appreciated

James

Time.now.to_i

Maurício Linhares
http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr

On Wed, Aug 19, 2009 at 12:35 PM, James

You want to use Time.now.to_f if you’re worried about more than one
hit per second.

On Aug 19, 11:44 am, Maurício Linhares [email protected]

Rick Lloyd wrote:

You want to use Time.now.to_f if you’re worried about more than one
hit per second.

On Aug 19, 11:44�am, Maur�cio Linhares [email protected]

Thank you for the fast response
Time.now.to_f gives me the following

My partial looks like this

<%tnow = Time.now.to_f%>
<%-list_form_id = “list-form-#{tnow}”-%>
<%-edit_form_id = “edit-form-#{tnow}”-%>

> <%= render :partial => 'address_list', :object => f.object, :locals => {:edit_form_div => edit_form_id} -%>
style="display: none"> etc...

I’ll move the div id generation out into a helper when I have this
working but I’m totally stumped

James W. 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
[email protected]

This certainly looks like an incorrect approach to the problem.
Could you, please, elaborate on this?

James W. wrote:

Rick Lloyd wrote:

You want to use Time.now.to_f if you’re worried about more than one
hit per second.

On Aug 19, 11:44�am, Maur�cio Linhares [email protected]

Thank you for the fast response
Time.now.to_f gives me the following

My partial looks like this

<%tnow = Time.now.to_f%>
<%-list_form_id = “list-form-#{tnow}”-%>
<%-edit_form_id = “edit-form-#{tnow}”-%>

> <%= render :partial => 'address_list', :object => f.object, :locals => {:edit_form_div => edit_form_id} -%>
style="display: none"> etc...

I’ll move the div id generation out into a helper when I have this
working but I’m totally stumped

My log is showing that these partials are being generagted really
quickly
Rendered admin/users/_address_list (16.0ms)
Rendered admin/users/_address_form (47.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (16.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (0.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (15.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (0.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (16.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (0.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (16.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (0.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (15.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (0.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (16.0ms)
Rendered admin/users/_address_list (0.0ms)
Rendered admin/users/_address_form (0.0ms)

So it looks like I need some kind of randomisation as well.

Gleb M. wrote:

This certainly looks like an incorrect approach to the problem.
Could you, please, elaborate on this?

That wouldn’t surprise me at all. lol!

It’s quite a complicated scenario and I was trying to keep this simple.

Here goes.

I’m working on a multi model form solution.
The theory.
One form that edits a user and can add addresses and email contact
details and will be extended to make use of other relationships.
The addresses and emails are shown as a list but each one has it’s own
form for editing using fields_for
I have edit links that show and hide the editing forms for each object
in the list
I can use the object_id for the divs for existing objects but that won’t
work for newly created objects

The code
edit_html.erb

Editing <%= show_user_type %>

<% form_for([:admin, @user]) do |f| %>

<%= render :partial => “form”, :locals => {:f => f}%>

<%= f.submit "Update" %>

<% end %>

<%= link_to ‘Show’, [:admin, @user] %> |
<%= link_to ‘Back’, admin_users_path %>

_form.html.erb
<%= f.error_messages -%>

<%= show_user_type %>

<%= f.label :name -%>: <%= f.text_field :name -%>

<%= f.label :user_name -%>: <%= f.text_field :user_name -%>

<%= f.label :password, 'Password' -%>: <%= f.password_field :password, :size => 40-%>

<%= f.label :password_confirmation, "confirm" -%>: <%= f.password_field :password_confirmation, :size => 40-%>

<%= add_address_link(f) %>

Addresses

<%= link_to_toggle_child_list('Addresses', 'address_list') %>
    <% f.fields_for :addresses do |address_form| -%> <%= render :partial => 'address_form', :locals => { :f => address_form } %> <% end -%>

link_to_toggle_child_list helper

def link_to_toggle_child_list(div_to_toggle, link_text)
link_to_function “Show/Hide #{link_text}” do |page|
page << “jQuery(’##{div_to_toggle}’).toggle(‘slow’)”
end
end

Add address link helper

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 })
page << “jQuery(’#address_list
ul’).append(’#{escape_javascript(html)}’.replace(/NEW_RECORD/g, new
Date().getTime()))”
end
end
end

The address form itself which is where the unique ID’s are needed
<%- if f.object.new_record?-%>
<%tnow = Time.now.to_f%>
<%-list_form_id = “list-form-#{tnow}”-%>
<%-edit_form_id = “edit-form-#{tnow}”-%>
<%else%>
<%-list_form_id = “list-form-#{f.object.id}”-%>
<%-edit_form_id = “edit-form-#{f.object.id}”-%>
<%-end-%>

> <%= render :partial => 'address_list', :object => f.object, :locals => {:edit_form_div => edit_form_id} -%>
style="display: none"> Details...

House name/number
<%= f.text_field :house -%>

Street
<%= f.text_field :street -%>

Town
<%= f.text_field :town -%>

city
<%= f.text_field :city -%>

Postcode
<%= f.text_field :postcode -%>

<%= link_to_function "Close", "jQuery('##{edit_form_id}').hide('slow')"-%>

So the actual _address_form.html.erb acts as both a single item in the
list and also the form which is hidden to start with and shown if a user
wants to edit the details so it “grows” into place underneath the item
in the list.
I thought this would be a kinda neat solution to my problem of the form
being too large to edit inline.
It’s the toggling of this div that I really need the ID for.

I also realise that I need to replace the UL and li tags with legal HTML
and will probably just add some styling for the lists in my style sheets
but there’s no point worrying about that until I at least have something
working.

By the way. The above code all works really nicely except for the new
records because of the ID not being unique if I add more than one
address (and the same goes for eMails) I no longer have a unique ID and
the wrong edit form is shown.

Hope that makes sense and if you have an alternative suggestion I’d love
to hear it :slight_smile:

On Aug 19, 1:07 pm, James W. [email protected]
wrote:
[…]

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

.

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
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

James W. 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
[email protected]

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

Marnen Laibow-Koser wrote:

On Aug 19, 1:07�pm, James W. [email protected]
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
[email protected]

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.

Marnen Laibow-Koser wrote:

On Aug 19, 1:07�pm, James W. [email protected]
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

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]