Skip to content

Instantly share code, notes, and snippets.

@maxim
Created March 13, 2009 21:56
Show Gist options
  • Select an option

  • Save maxim/78791 to your computer and use it in GitHub Desktop.

Select an option

Save maxim/78791 to your computer and use it in GitHub Desktop.
# MODEL
class Vocabulary < ActiveRecord::Base
has_many :terms, :dependent => :destroy
accepts_nested_attributes_for :terms,
:allow_destroy => true,
:reject_if => proc { |attrs| attrs[:name].blank? }
end
# HELPER
module VocabulariesHelper
def setup_vocabulary(vocabulary)
returning(vocabulary) do |v|
3.times { v.terms.build }
end
end
end
# VIEW
<% form_for setup_vocabulary(@vocabulary), :html => { :class => "std-form short" } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %>
<%= f.text_field :title, :class => 'text' %>
</p>
<fieldset id="terms">
<legend>Terms:</legend>
<% f.fields_for :terms do |term_form| %>
<div class="term">
<p>
<%= term_form.label :name, "Term:" %>
<%= term_form.text_field :name, :class => 'text' %>
<% unless term_form.object.new_record? %>
<%= term_form.check_box '_delete' %>
<%= term_form.label '_delete', 'Remove' %>
<% end %>
</p>
</div>
<% end %>
</fieldset>
<p>
<%= f.submit @vocabulary.new_record? ? "Create" : "Update" %>
</p>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment