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?
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?
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
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
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.