James West
2009-Aug-19 15:35 UTC
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 -- Posted via http://www.ruby-forum.com/.
Maurício Linhares
2009-Aug-19 15:44 UTC
Re: How to get current time including milliseconds for div id?
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 West<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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%>> > > 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 > -- > Posted via http://www.ruby-forum.com/. > > > >
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Time.now.to_i > > - > Maurício Linhareshttp://codeshooter.wordpress.com/|http://twitter.com/mauriciojr > > On Wed, Aug 19, 2009 at 12:35 PM, James > > West<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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%>> > > > 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 > > -- > > Posted viahttp://www.ruby-forum.com/.
James West
2009-Aug-19 16:13 UTC
Re: How to get current time including milliseconds for div id?
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 -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Aug-19 16:17 UTC
Re: 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 http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
James West
2009-Aug-19 16:19 UTC
Re: How to get current time including milliseconds for div id?
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 stumpedMy 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. -- Posted via http://www.ruby-forum.com/.
Gleb Mazovetskiy
2009-Aug-19 16:26 UTC
Re: How to get current time including milliseconds for div
This certainly looks like an incorrect approach to the problem. Could you, please, elaborate on this? -- Posted via http://www.ruby-forum.com/.
James West
2009-Aug-19 17:05 UTC
Re: How to get current time including milliseconds for div
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 :-) -- Posted via http://www.ruby-forum.com/.
James West
2009-Aug-19 17:07 UTC
Re: How to get current time including milliseconds for div id?
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-sbuyVjPbboAdnm+yROfE0A@public.gmane.orgGUIDS may well be the solution I need :-) Any idea howe to create them :-) See my earlier response for the reason why I need them. And thank you for taking so much trouble to understand my problem -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Aug-19 17:21 UTC
Re: How to get current time including milliseconds for div id?
On Aug 19, 1:07 pm, James West <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: [...]> GUIDS may well be the solution I need :-) > > Any idea howe to create them :-)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 problemYou''re welcome! Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org
James West
2009-Aug-19 18:10 UTC
Re: How to get current time including milliseconds for div id?
Marnen Laibow-Koser wrote:> On Aug 19, 1:07�pm, James West <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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>> > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org-- Posted via http://www.ruby-forum.com/.
James West
2009-Aug-19 18:50 UTC
Re: How to get current time including milliseconds for div id?
Marnen Laibow-Koser wrote:> On Aug 19, 1:07�pm, James West <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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-sbuyVjPbboAdnm+yROfE0A@public.gmane.orgProblem 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. -- Posted via http://www.ruby-forum.com/.