This repository has been archived by the owner on Apr 9, 2019. It is now read-only.
forked from ryanb/complex-form-examples
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
63 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,6 +1,6 @@ | |||
Complex Form Examples | Complex Form Examples | ||
--------------------- | --------------------- | ||
|
|
||
Several different ways to handle the multi-model form problem with a | This one is copied directly from the Advanced Rails Recipes book. | ||
one-to-many association. The various approaches are in git branches. |
|
||
See them for examples. | http://www.pragprog.com/titles/fr_arr/advanced-rails-recipes |
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,2 +1,7 @@ | |||
module ProjectsHelper | module ProjectsHelper | ||
def add_task_link(name) | |||
link_to_function name do |page| | |||
page.insert_html :bottom, :tasks, :partial => 'task', :object => Task.new | |||
end | |||
end | |||
end | end |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,31 @@ | |||
class Project < ActiveRecord::Base | class Project < ActiveRecord::Base | ||
has_many :tasks | has_many :tasks, :dependent => :destroy | ||
|
|||
validates_presence_of :name | validates_presence_of :name | ||
validates_associated :tasks | |||
|
|||
after_update :save_tasks | |||
|
|||
def new_task_attributes=(task_attributes) | |||
task_attributes.each do |attributes| | |||
tasks.build(attributes) | |||
end | |||
end | |||
|
|||
def existing_task_attributes=(task_attributes) | |||
tasks.reject(&:new_record?).each do |task| | |||
attributes = task_attributes[task.id.to_s] | |||
if attributes | |||
task.attributes = attributes | |||
else | |||
tasks.delete(task) | |||
end | |||
end | |||
end | |||
|
|||
def save_tasks | |||
tasks.each do |task| | |||
task.save(false) | |||
end | |||
end | |||
end | end |
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,8 +1,16 @@ | |||
<%= error_messages_for :project %> | <%= error_messages_for :project %> | ||
<% form_for @project do |f| %> | |||
<% form_for @project do |f| -%> | |||
<p> | <p> | ||
<%= f.label :name %><br /> | Name: <%= f.text_field :name %> | ||
<%= f.text_field :name %> | |||
</p> | </p> | ||
<p><%= f.submit "Submit" %></p> | <div id="tasks"> | ||
<% end %> | <%= render :partial => 'task', :collection => @project.tasks %> | ||
</div> | |||
<p> | |||
<%= add_task_link "Add a task" %> | |||
</p> | |||
<p> | |||
<%= f.submit "Submit" %> | |||
</p> | |||
<% end -%> |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
<div class="task"> | |||
<% new_or_existing = task.new_record? ? 'new' : 'existing' %> | |||
<% prefix = "project[#{new_or_existing}_task_attributes][]" %> | |||
<% fields_for prefix, task do |task_form| -%> | |||
<p> | |||
Task: <%= task_form.text_field :name %> | |||
<%= link_to_function "remove", "$(this).up('.task').remove()" %> | |||
</p> | |||
<% end -%> | |||
</div> |