Skip to content

Instantly share code, notes, and snippets.

@chrislerum
Created June 8, 2010 16:20
Show Gist options
  • Save chrislerum/430257 to your computer and use it in GitHub Desktop.
Save chrislerum/430257 to your computer and use it in GitHub Desktop.
class Branch < ActiveRecord::Base
belongs_to :tree
has_many :leaves
validates_presence_of :name, :message => "A branch without a name? WTF?"
validates_presence_of :tree_id, :message => "was not there dude."
end
Parameters: {"commit"=>"Save changes", "authenticity_token"=>"YnGd4rKBCki4+r6sNcwfDlgR5Ur+O7emsQZkGvJu/zw=", "tree"=>{"name"=>"Hank the Tree", "branches_attributes"=>{"0"=>{"name"=>"Bob the Branch"}}}}
New Tree
<br />
<% form_for setup_tree(@tree) do |tree_form| %>
<%= tree_form.error_messages %>
<%= tree_form.label :name, "Tree Name:" %>
<%= tree_form.text_field :name %>
<br /><br />
Add a branch:
<br />
<% tree_form.fields_for :branches do |branch_form| %>
<%= branch_form.label :name, "Branch Name:" %>
<%= branch_form.text_field :name %>
<% end %>
<br />
<%= tree_form.submit %>
<% end %>
<form action="/trees" class="new_tree" id="new_tree" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="YnGd4rKBCki4+r6sNcwfDlgR5Ur+O7emsQZkGvJu/zw=" /></div>
<label for="tree_name">Tree Name:</label>
<input id="tree_name" name="tree[name]" size="30" type="text" />
<br /><br />
Add a branch:
<br />
<label for="tree_branches_attributes_0_name">Branch Name:</label>
<input id="tree_branches_attributes_0_name" name="tree[branches_attributes][0][name]" size="30" type="text" />
<br />
<input id="tree_submit" name="commit" type="submit" value="Save changes" />
</form>
class Tree < ActiveRecord::Base
has_many :branches
validates_presence_of :name
accepts_nested_attributes_for :branches, :allow_destroy => true, :reject_if => :all_blank
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment