Skip to content

Instantly share code, notes, and snippets.

@felipemesquita
Created May 18, 2012 19:23
Show Gist options
  • Save felipemesquita/2727176 to your computer and use it in GitHub Desktop.
Save felipemesquita/2727176 to your computer and use it in GitHub Desktop.
<%= simple_form_for(@project) do |project_form| %>
<ul id="goals">
<%= project_form.simple_fields_for :goals do |goal_form| %>
<%= render 'goal_form', :goal_form => goal_form %>
<% end %>
</ul>
<% end %>
<% el_id = goal_form.object.new_record?? 'NEW_RECORD' : goal_form.object.object_id %>
<li id="<%= el_id %>">
<%= goal_form.input :name %>
</li>
class Project < ActiveRecord::Base
has_many :goals, :dependent => :destroy
accepts_nested_attributes_for :goals, :allow_destroy => true
end
function add_goal(html_form) {
$('#goals').append( html_form.replace(/NEW_RECORD/g, new Date().getTime()) );
}
def add_goal_link(form_builder)
html = nil
form_builder.fields_for :goals, Goal.new, :child_index => 'NEW_RECORD' do |goal_form|
html = render('goal_form', :goal_form => goal_form )
end
link_to_function t('interface.add_goal'), "add_goal('#{escape_javascript(html)}')"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment