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 