Skip to content

Instantly share code, notes, and snippets.

@veritas1
Created December 30, 2012 16:41
Show Gist options
  • Save veritas1/4413716 to your computer and use it in GitHub Desktop.
Save veritas1/4413716 to your computer and use it in GitHub Desktop.
For my blog post, using 'accepts_nested_attributes_for' and adding fields dynamically.
function add_field(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
function remove_field = (link) {
$(link).siblings("input[type=hidden]").val(1);
$(link).closest(".fieldset").hide();
}
= form_for @post do |f|
// skipped
= f.fields_for :paragraphs do |builder|
// skipped
class Post < ActiveRecord::Base
attr_accessible :subtitle, :title, :paragraphs_attributes
has_many :paragraphs, dependent: :destroy
accepts_nested_attributes_for :paragraphs, allow_destroy: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment