Browse files

Merge ryanb's changes

  • Loading branch information...
2 parents f308ebd + 3d36b80 commit 29ac72151556b73c8b84875dfebc47e134d7becd @timriley committed Oct 15, 2009
Showing with 15 additions and 16 deletions.
  1. +2 −2 LICENSE
  2. +3 −3 README.rdoc
  3. +6 −7 app/helpers/application_helper.rb
  4. +4 −4 public/javascripts/application.js
View
4 LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2008 Ryan Bates
+Copyright (c) 2009 Ryan Bates
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
6 README.rdoc
@@ -1,8 +1,6 @@
= Complex Form Examples
-This is an adaptation of Ryan Bates' simple solution for the multi-model form problem using the latest +accepts_nested_attributes_for+ available in Rails 2.3.
-
-It uses unobtrusive jQuery and HTML5.
+A simple solution for the multi-model form problem using the latest +accepts_nested_attributes_for+ available in Rails 2.3. This version uses unobtrusive JavaScript with jQuery and HTML 5.
Once you clone this repository, just run the migrations and start up the server to try it out.
@@ -12,3 +10,5 @@ Once you clone this repository, just run the migrations and start up the server
See the branches for alternative solutions. Here's how to clone a remote branch.
git checkout -b deep origin/deep
+
+Special thanks to Tim Riley and Eloy Duran.
View
13 app/helpers/application_helper.rb
@@ -1,23 +1,22 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
def remove_child_link(name, f)
- f.hidden_field(:_delete) + "<a href=\"javascript:void(0)\" class=\"remove_child\">#{name}</a>"
+ f.hidden_field(:_delete) + link_to(name, "javascript:void(0)", :class => "remove_child")
end
def add_child_link(name, association)
- "<a href=\"javascript:void(0)\" class=\"add_child\" data-association=\"#{association.to_s}\">#{name}</a>"
+ link_to(name, "javascript:void(0)", :class => "add_child", :"data-association" => association)
end
def new_child_fields_template(form_builder, association, options = {})
options[:object] ||= form_builder.object.class.reflect_on_association(association).klass.new
options[:partial] ||= association.to_s.singularize
options[:form_builder_local] ||= :f
- ret = "<div id=\"#{association}_fields_template\" style=\"display: none\">"
- form_builder.fields_for(association, options[:object], :child_index => "new_#{association}") do |f|
- ret += render(:partial => options[:partial], :locals => {options[:form_builder_local] => f})
+ content_tag(:div, :id => "#{association}_fields_template", :style => "display: none") do
+ form_builder.fields_for(association, options[:object], :child_index => "new_#{association}") do |f|
+ render(:partial => options[:partial], :locals => {options[:form_builder_local] => f})
+ end
end
- ret += "</div>"
- ret
end
end
View
8 public/javascripts/application.js
@@ -1,15 +1,15 @@
-$(document).ready(function() {
- $('.add_child').click(function() {
+$(function() {
+ $('form a.add_child').click(function() {
var assoc = $(this).attr('data-association');
var content = $('#' + assoc + '_fields_template').html();
- var regexp = new RegExp('new_' + assoc, 'g')
+ var regexp = new RegExp('new_' + assoc, 'g');
var new_id = new Date().getTime();
$(this).parent().before(content.replace(regexp, new_id));
return false;
});
- $('.remove_child').live('click', function() {
+ $('form a.remove_child').live('click', function() {
var hidden_field = $(this).prev('input[type=hidden]')[0];
if(hidden_field) {
hidden_field.value = '1';

0 comments on commit 29ac721

Please sign in to comment.