This example is perfect. However, for some reason, it isn't working.
When I click the Add Light link it doesn't add any fields.  Here is
all the relevant code (I think).  Basically, for me, project is room,
and task is light.

----------------------------------------------------

<!-- app/views/rooms/new.html.erb -->
<h1>New room</h1>

<%= render :partial => 'room_form' %>

<p><%= link_to "Back to List", rooms_path %></p>

----------------------------------------------------

----------------------------------------------------

<!-- app/views/rooms/_room_form.html.erb -->
<% form_for @room do |form| %>
  <%= form.error_messages %>
  <p>
    <%= form.label :name %><br />
    <%= form.text_field :name %>
  </p>

  <h3>Lights</h3>
  <% form.fields_for :lights do |light_form| %>
    <%= render :partial => 'light', :locals => { :form => light_form }
%>
  <% end %>
  <p><%= add_child_link "[+] Add new light", form, :lights %></p>
  <p><%= form.submit "Submit" %></p>
<% end %>

----------------------------------------------------

----------------------------------------------------

<!-- app/views/rooms/_light.html.erb -->
<div class="tier2">
  <%= form.label :wattage, "Wattage of lights: " %>
  <%= form.text_field :wattage, :size => 2 %><br />
  <%= form.label :quantity, "Number of lights: " %>
  <%= form.text_field :quantity, :size => 2 %><br />
  <%= form.label :hours_used_per_day, "Number of hours per day lights
are on: " %>
  <%= form.text_field :hours_used_per_day, :size => 2 %><br />
  <%= remove_child_link "remove", form %>
</div><br />

----------------------------------------------------

----------------------------------------------------

# app/helpers/application_helper.rb

# Methods added to this helper will be available to all templates in
the application.
module ApplicationHelper
  def remove_child_link(wattage, form)
    form.hidden_field(:_delete) + link_to_function(wattage,
"remove_fields(this)")
  end

  def add_child_link(wattage, form, method)
    fields = new_child_fields(form, method)
    link_to_function(wattage, h("insert_fields(this, \"#{method}\", \"#
{escape_javascript(fields)}\")"))
  end

  def new_child_fields(form_builder, method, options = {})
    options[:object] ||=
form_builder.object.class.reflect_on_association(method).klass.new
    options[:partial] ||= method.to_s.singularize
    options[:form_builder_local] ||= :form
    form_builder.fields_for(method, options[:object], :child_index =>
"new_#{method}") do |form|
      render(:partial => options[:partial], :locals => { options
[:form_builder_local] => form })
    end
  end
end

----------------------------------------------------

Any idea why add_child_link isn't working?

Thanks,
Ryan
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to