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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to