Permalink
Browse files

pulling in some of the changes from deep branch

  • Loading branch information...
1 parent 3bb8ec1 commit 4e4623659ba56cc338eb25bde8d1af55d4fb026c @ryanb committed Aug 5, 2009
View
10 app/helpers/application_helper.rb
@@ -1,19 +1,19 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
- def remove_task_link(name, f)
+ def remove_child_link(name, f)
f.hidden_field(:_delete) + link_to_function(name, "remove_fields(this)")
end
- def add_task_link(name, f)
- fields = new_child_fields(f, :tasks)
- link_to_function(name, "insert_fields('tasks', '#{escape_javascript(fields)}')")
+ def add_child_link(name, f, method)
+ fields = new_child_fields(f, method)
+ link_to_function(name, 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] ||= :f
- form_builder.fields_for(method, options[:object], :child_index => 'NEW_RECORD') do |f|
+ form_builder.fields_for(method, options[:object], :child_index => "new_#{method}") do |f|
render(:partial => options[:partial], :locals => { options[:form_builder_local] => f })
end
end
View
4 app/views/projects/_form.html.erb
@@ -6,11 +6,9 @@
</p>
<h3>Tasks</h3>
- <div id="tasks">
<% f.fields_for :tasks do |task_form| %>
<%= render :partial => 'task', :locals => { :f => task_form } %>
<% end %>
- </div>
- <p><%= add_task_link "New Task", f %></p>
+ <p><%= add_child_link "New Task", f, :tasks %></p>
<p><%= f.submit "Submit" %></p>
<% end %>
View
2 app/views/projects/_task.html.erb
@@ -1,7 +1,7 @@
<div class="field">
<%= f.label :name, "Task" %>
<%= f.text_field :name %>
- <%= remove_task_link "remove", f %>
+ <%= remove_child_link "remove", f %>
<%# Alternatively you can use a check box: %>
<%# f.check_box :_delete %>
<%# f.label :_delete, "(remove)" %>
View
9 public/javascripts/application.js
@@ -1,9 +1,10 @@
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
-function insert_fields(element_id, content) {
+function insert_fields(link, method, content) {
var new_id = new Date().getTime();
- $(element_id).insert({
- bottom: content.replace(/NEW_RECORD/g, new_id)
+ var regexp = new RegExp("new_" + method, "g")
+ $(link).up().insert({
+ before: content.replace(regexp, new_id)
});
}
@@ -12,5 +13,5 @@ function remove_fields(link) {
if (hidden_field) {
hidden_field.value = '1';
}
- $(link).up(".field").remove();
+ $(link).up(".field").hide();
}

0 comments on commit 4e46236

Please sign in to comment.