Created
September 1, 2011 15:22
-
-
Save marclipovsky/1186408 to your computer and use it in GitHub Desktop.
nested_forms creating default tasks from category tasks
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
<!-- START category.rb --> | |
class Category < ActiveRecord::Base | |
has_many :tickets | |
has_many :tasks | |
accepts_nested_attributes_for :tasks, :allow_destroy => true, :reject_if => proc { |obj| obj.blank? } | |
end | |
<!-- END category.rb --> | |
<!-- START ticket.rb --> | |
class Ticket < ActiveRecord::Base | |
belongs_to :category | |
has_and_belongs_to_many :accounts | |
has_many :tasks | |
accepts_nested_attributes_for :tasks, :allow_destroy => true, :reject_if => proc { |obj| obj.blank? } | |
validates_presence_of :title, :status, :requestor_id, :starts, :target, :message => "Can't be blank" | |
end | |
<!-- END ticket.rb --> | |
<!-- START task.rb --> | |
class Task < ActiveRecord::Base | |
belongs_to :category | |
belongs_to :ticket | |
end | |
<!-- END task.rb --> | |
<!-- START _form.html.erb (Category) --> | |
<%= nested_form_for @category do |f| %> | |
<%= f.error_messages %> | |
<p> | |
<%= f.label :title %><br /> | |
<%= f.text_field :title %> | |
</p> | |
<p> | |
<%= f.label :description %><br /> | |
<%= f.text_area :description, :rows => 3 %> | |
</p> | |
<% if @category.parent_category_id? %> | |
<p> | |
<%= collection_select(:category, :parent_category_id, Category.where(:parent_category_id => nil), :id, :title, :include_blank => true) %> | |
</p> | |
<% end -%> | |
<div id="add_default_tasks"> | |
<h2>Default Tasks</h2> | |
<%= f.fields_for :tasks do |task_form| %> | |
<%= task_form.text_field :name %> | |
<%= task_form.link_to_remove "Remove this task", :class => "remove_task" %> | |
<% end %> | |
<p><%= f.link_to_add "Add a task", :tasks, :class => "add_task" %></p> | |
<div class="clear"></div> | |
</div> | |
<p><%= f.submit %></p> | |
<% end %> | |
<!-- END _form.html.erb (Category) --> | |
<!-- START _form.html.erb (Ticket) --> | |
<%= nested_form_for @ticket, :validate => true do |f| %> | |
<p> | |
<%= f.label :title %><br /> | |
<%= f.text_field :title, :autocomplete => "off" %> | |
</p> | |
<p> | |
<%= f.label :category_id, "Select a Category" %> | |
<select id="ticket_category_id" name="ticket[category_id]"> | |
<option value=""></option> | |
<% for category in Category.where("categories.parent_category_id IS NULL") %> | |
<option value="<%= category.id %>"><%= category.title %></option> | |
<% if !Category.where('parent_category_id = ?', category.id).blank? %> | |
<% for child_cat in Category.where('parent_category_id = ?', category.id) %> | |
<option value="<%= child_cat.id %>"> - <%= child_cat.title %></option> | |
<% end %> | |
<% end %> | |
<% end %> | |
</select> | |
</p> | |
<div id="add_default_tasks"> | |
<h2>Default Tasks</h2> | |
<%= f.fields_for :tasks do |task_form| %> | |
<% if @ticket.tasks.blank? && [email protected]? && [email protected]? %> | |
<% for task in @ticket.category.tasks %> | |
<%= task_form.text_field :name, :value => task.name %> | |
<%= task_form.link_to_remove "Remove this task", :class => "remove_task" %> | |
<% end %> | |
<% elsif [email protected]? %> | |
<% for task in @ticket.tasks %> | |
<%= task_form.text_field :name, :value => task.name %> | |
<%= task_form.link_to_remove "Remove this task", :class => "remove_task" %> | |
<% end %> | |
<% else %> | |
<%= 3.times do %> | |
<%= task_form.text_field :name, :value => task.name %> | |
<%= task_form.link_to_remove "Remove this task", :class => "remove_task" %> | |
<% end %> | |
<% end %> | |
<% end %> | |
<p><%= f.link_to_add "Add a task", :tasks, :class => "add_task" %></p> | |
<div class="clear"></div> | |
</div> | |
<%# ... %> | |
<p><%= f.submit %></p> | |
<% end %> | |
<!-- END _form.html.erb (Ticket) --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment