Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add tasks

  • Loading branch information...
commit d2d85cff71d92ff182556d0bc1a94cacbe86b5b1 1 parent 5ef870b
Xavier Shay authored
3  app/helpers/form_helper.rb
@@ -5,6 +5,9 @@ def setup_user(user)
5 5 user.interest_users.build(:interest => interest)
6 6 end
7 7 user.interest_users.sort_by! {|x| x.interest.name }
  8 +
  9 + 3.times { user.tasks.build }
  10 +
8 11 user
9 12 end
10 13 end
5 app/models/task.rb
... ... @@ -0,0 +1,5 @@
  1 +class Task < ActiveRecord::Base
  2 + belongs_to :user
  3 +
  4 + validates_presence_of :name
  5 +end
2  app/models/user.rb
@@ -2,9 +2,11 @@ class User < ActiveRecord::Base
2 2 has_one :address
3 3 has_many :interest_users
4 4 has_many :interests, :through => :interest_users
  5 + has_many :tasks
5 6
6 7 accepts_nested_attributes_for :address
7 8 accepts_nested_attributes_for :interest_users, :allow_destroy => true
  9 + accepts_nested_attributes_for :tasks, :allow_destroy => true, :reject_if => :all_blank
8 10
9 11 validates_presence_of :email
10 12 end
15 app/views/users/_form.html.erb
@@ -25,13 +25,26 @@
25 25
26 26 <%= f.fields_for :interest_users do |ff| %>
27 27 <div>
28   - <%= ff.hidden_field :id %>
29 28 <%= ff.hidden_field :interest_id %>
30 29 <%= ff.check_box :_destroy, {:checked => ff.object.persisted?}, '0', '1' %>
31 30 <%= ff.label :_destroy, ff.object.interest.name %>
32 31 </div>
33 32 <% end %>
34 33
  34 + <h2>Tasks</h2>
  35 + <%= f.fields_for :tasks do |ff| %>
  36 + <div>
  37 + <%= ff.label :name %>
  38 + <%= ff.text_field :name %>
  39 +
  40 + <% if ff.object.persisted? %>
  41 + <%= ff.check_box :_destroy %>
  42 + <%= ff.label :_destroy, "Destroy" %>
  43 + <% end %>
  44 + </div>
  45 +
  46 + <% end %>
  47 +
35 48 <div class="actions">
36 49 <%= f.submit %>
37 50 </div>
6 db/migrate/20110306221257_create_models.rb
@@ -24,6 +24,12 @@ def self.up
24 24 t.references :user
25 25 t.references :interest
26 26 end
  27 +
  28 + create_table :tasks do |t|
  29 + t.references :user
  30 +
  31 + t.string :name, :null => false
  32 + end
27 33 end
28 34
29 35 def self.down
5 db/schema.rb
@@ -26,6 +26,11 @@
26 26 t.string "name", :null => false
27 27 end
28 28
  29 + create_table "tasks", :force => true do |t|
  30 + t.integer "user_id"
  31 + t.string "name", :null => false
  32 + end
  33 +
29 34 create_table "users", :force => true do |t|
30 35 t.string "email", :null => false
31 36 t.datetime "created_at"

0 comments on commit d2d85cf

Please sign in to comment.
Something went wrong with that request. Please try again.