How to get current time including milliseconds for div id?

Time.now.to_i

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

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 <linhares.mauri...@gmail.com>

Thank you for the fast response
Time.now.to_f gives me the following
<div id="edit-form-1250697708.562" style="display: none;">
</div>
<div id="list-form-1250697708.562">
</div>
<div id="edit-form-1250697708.562" style="display: none;">
</div>
<div id="list-form-1250697708.562">
</div>
<div id="edit-form-1250697708.562" style="display: none;">
</div>
<div id="list-form-1250697708.562">
</div>
<div id="edit-form-1250697708.562" style="display: none;">
</div>

My partial looks like this

<%tnow = Time.now.to_f%>
<%-list_form_id = "list-form-#{tnow}"-%>
<%-edit_form_id = "edit-form-#{tnow}"-%>
<div id =<%=list_form_id-%>>
  <%= render :partial => 'address_list', :object => f.object, :locals =>
{:edit_form_div => edit_form_id} -%>
</div>
<div id =<%=edit_form_id-%> style="display: none">
  <fieldset>
etc...

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

James West 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 <linhares.mauri...@gmail.com>

Thank you for the fast response
Time.now.to_f gives me the following
<div id="edit-form-1250697708.562" style="display: none;">
</div>
<div id="list-form-1250697708.562">
</div>
<div id="edit-form-1250697708.562" style="display: none;">
</div>
<div id="list-form-1250697708.562">
</div>
<div id="edit-form-1250697708.562" style="display: none;">
</div>
<div id="list-form-1250697708.562">
</div>
<div id="edit-form-1250697708.562" style="display: none;">
</div>

My partial looks like this

<%tnow = Time.now.to_f%>
<%-list_form_id = "list-form-#{tnow}"-%>
<%-edit_form_id = "edit-form-#{tnow}"-%>
<div id =<%=list_form_id-%>>
  <%= render :partial => 'address_list', :object => f.object, :locals =>
{:edit_form_div => edit_form_id} -%>
</div>
<div id =<%=edit_form_id-%> style="display: none">
  <fieldset>
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.

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

Gleb Mazovetskiy 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
<h3>Editing <%= show_user_type %> </h3>

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

  <%= render :partial => "form", :locals => {:f => f}%>
  <p>
  <%= f.submit "Update" %>
  </p>
<% end %>

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

_form.html.erb
<%= f.error_messages -%>
<div class="admin-form">
  <fieldset>
    <legend><%= show_user_type %></legend>
    <p>
      <%= f.label :name -%>:
      <%= f.text_field :name -%>
    </p>
    <p>
      <%= f.label :user_name -%>:
      <%= f.text_field :user_name -%>
    </p>
    <p>
      <%= f.label :password, 'Password' -%>:
      <%= f.password_field :password, :size => 40-%>
    </p>
    <p>
      <%= f.label :password_confirmation, "confirm" -%>:
      <%= f.password_field :password_confirmation, :size => 40-%>
    </p>
  </fieldset>
</div>

<%= add_address_link(f) %>
<h3>Addresses</h3>
<%= link_to_toggle_child_list('Addresses', 'address_list') %><!-- This
will show or hide the whole list -->
<div id ="address_list">
  <ul>
     <% f.fields_for :addresses do |address_form| -%>
       <%= render :partial => 'address_form', :locals => { :f =>
address_form } %>
     <% end -%>
  </ul>
</div>

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-%>
<div id =<%=list_form_id-%>>
  <%= render :partial => 'address_list', :object => f.object, :locals =>
{:edit_form_div => edit_form_id} -%>
</div>
<div id =<%=edit_form_id-%> style="display: none">
  <fieldset>
    <legend>Details...</legend>
      <p>
        House name/number<br />
        <%= f.text_field :house -%>
      </p>
      <p>
        Street<br />
        <%= f.text_field :street -%>
      </p>
      <p>
        Town<br />
        <%= f.text_field :town -%>
      </p>
      <p>
        city<br />
        <%= f.text_field :city -%>
      </p>
      <p>
        Postcode<br />
        <%= f.text_field :postcode -%>
      </p>
    <%= link_to_function "Close",
      "jQuery('##{edit_form_id}').hide('slow')"-%>
  </fieldset>
</div>

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: