Created
December 30, 2012 16:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= form_for @post do |f| | |
// skipped | |
= f.fields_for :paragraphs do |builder| | |
// skipped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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