Skip to content
This repository has been archived by the owner on Apr 9, 2019. It is now read-only.

Commit

Permalink
adding code from book
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Jul 18, 2008
1 parent 65f95b2 commit e397c44
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README
@@ -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
2 changes: 2 additions & 0 deletions app/controllers/projects_controller.rb
Expand Up @@ -9,6 +9,7 @@ def show


def new def new
@project = Project.new @project = Project.new
@project.tasks.build
end end


def create def create
Expand All @@ -26,6 +27,7 @@ def edit
end end


def update def update
params[:project][:existing_task_attributes] ||= {}
@project = Project.find(params[:id]) @project = Project.find(params[:id])
if @project.update_attributes(params[:project]) if @project.update_attributes(params[:project])
flash[:notice] = "Successfully updated project." flash[:notice] = "Successfully updated project."
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/projects_helper.rb
@@ -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
29 changes: 28 additions & 1 deletion app/models/project.rb
@@ -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
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -4,6 +4,7 @@
<head> <head>
<title><%= h(yield(:title) || "Untitled") %></title> <title><%= h(yield(:title) || "Untitled") %></title>
<%= stylesheet_link_tag 'application' %> <%= stylesheet_link_tag 'application' %>
<%= javascript_include_tag :defaults %>
<%= yield(:head) %> <%= yield(:head) %>
</head> </head>
<body> <body>
Expand Down
18 changes: 13 additions & 5 deletions app/views/projects/_form.html.erb
@@ -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 -%>
11 changes: 11 additions & 0 deletions app/views/projects/_task.html.erb
@@ -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>

0 comments on commit e397c44

Please sign in to comment.