ScaffoldPlusAnswerThree » 履歴 » バージョン 16
Yuumi Yoshida, 2011-08-13 00:33
1 | 7 | Yuumi Yoshida | h1. 演習3解答例 |
---|---|---|---|
2 | |||
3 | |||
4 | |||
5 | h2. 作業手順 |
||
6 | |||
7 | <pre> |
||
8 | 12 | Yuumi Yoshida | rails generate model user name:string user_id:integer |
9 | rails generate migration AddUserIdToTodo user_id:integer |
||
10 | 1 | Yuumi Yoshida | rake db:migrate |
11 | 12 | Yuumi Yoshida | rails console # usersデータの作成,todos.user_id更新 |
12 | 16 | Yuumi Yoshida | u = User.new(:id=>1, :name=>'山田') |
13 | 6 | Yuumi Yoshida | u.save |
14 | u = User.new(:id=>2, :name=>'川田') |
||
15 | 1 | Yuumi Yoshida | u.save |
16 | Todo.update_all("user_id=1") |
||
17 | 12 | Yuumi Yoshida | User.all # users の確認 |
18 | Todo.all # todos の確認 |
||
19 | 1 | Yuumi Yoshida | quit |
20 | 7 | Yuumi Yoshida | </pre> |
21 | 1 | Yuumi Yoshida | |
22 | |||
23 | 7 | Yuumi Yoshida | h2. 変更点 |
24 | |||
25 | |||
26 | |||
27 | h3. 1. app/models/user.rb |
||
28 | 1 | Yuumi Yoshida | |
29 | Todoとの一対多の関連を記述 |
||
30 | 8 | Yuumi Yoshida | <pre><code class="color"> |
31 | class User < ActiveRecord::Base |
||
32 | ##(has_many :todos)## |
||
33 | 1 | Yuumi Yoshida | end |
34 | 8 | Yuumi Yoshida | </code></pre> |
35 | 1 | Yuumi Yoshida | |
36 | |||
37 | h3. 2. app/models/todo.rb |
||
38 | 7 | Yuumi Yoshida | |
39 | 1 | Yuumi Yoshida | Userとの多対一の関連(従属)を記述 |
40 | 8 | Yuumi Yoshida | <pre><code class="color"> |
41 | class Todo < ActiveRecord::Base |
||
42 | ##(belongs_to :user)## |
||
43 | 2 | Yuumi Yoshida | end |
44 | 8 | Yuumi Yoshida | </code></pre> |
45 | 2 | Yuumi Yoshida | |
46 | 7 | Yuumi Yoshida | |
47 | h3. 3. app/views/todos/index.html.erb |
||
48 | |||
49 | 1 | Yuumi Yoshida | user.nameカラム表示用のコードを追加 |
50 | 8 | Yuumi Yoshida | <pre><code class="color"> |
51 | <h1>Listing todos</h1> |
||
52 | 3 | Yuumi Yoshida | |
53 | 8 | Yuumi Yoshida | <table> |
54 | <tr> |
||
55 | <th>Due</th> |
||
56 | ##(<th>Name</th>)## |
||
57 | <th>Task</th> |
||
58 | </tr> |
||
59 | 1 | Yuumi Yoshida | |
60 | 8 | Yuumi Yoshida | <%25 for todo in @todos %25> |
61 | <tr> |
||
62 | 14 | Yuumi Yoshida | <td><%25= todo.due %25></td> |
63 | ##(<td><%25= todo.user.name %25></td>)## |
||
64 | <td><%25= todo.task %25></td> |
||
65 | 8 | Yuumi Yoshida | <td><%25= link_to 'Show', todo %25></td> |
66 | <td><%25= link_to 'Edit', edit_todo_path(todo) %25></td> |
||
67 | <td><%25= link_to 'Destroy', todo, :confirm => 'Are you sure?', :method => :delete %25></td> |
||
68 | </tr> |
||
69 | <%25 end %25> |
||
70 | </table> |
||
71 | 1 | Yuumi Yoshida | |
72 | 8 | Yuumi Yoshida | <br /> |
73 | 1 | Yuumi Yoshida | |
74 | 8 | Yuumi Yoshida | <%25= link_to 'New todo', new_todo_path %25> |
75 | </code></pre> |
||
76 | 1 | Yuumi Yoshida | |
77 | 3 | Yuumi Yoshida | |
78 | h3. 4. app/views/todos/show.html.erb |
||
79 | |||
80 | user.nameカラム表示用のコードを追加 |
||
81 | 8 | Yuumi Yoshida | <pre><code class="color"> |
82 | 9 | Yuumi Yoshida | <p> |
83 | <b>Due:</b> |
||
84 | 14 | Yuumi Yoshida | <%25= @todo.due %25> |
85 | 9 | Yuumi Yoshida | </p> |
86 | |||
87 | <p> |
||
88 | <b>Task:</b> |
||
89 | 14 | Yuumi Yoshida | <%25= @todo.task %25> |
90 | 9 | Yuumi Yoshida | </p> |
91 | |||
92 | ##(<p> |
||
93 | <b>Name:</b> |
||
94 | 14 | Yuumi Yoshida | <%25= @todo.user.name %25> |
95 | 9 | Yuumi Yoshida | </p>)## |
96 | |||
97 | <p> |
||
98 | <b>Memo:</b><br/> |
||
99 | 14 | Yuumi Yoshida | <%25= new_line(@todo.memo) %25> |
100 | 9 | Yuumi Yoshida | </p> |
101 | |||
102 | |||
103 | <%25= link_to 'Edit', edit_todo_path(@todo) %25> | |
||
104 | 1 | Yuumi Yoshida | <%25= link_to 'Back', todos_path %25> |
105 | </code></pre> |
||
106 | |||
107 | |||
108 | 14 | Yuumi Yoshida | h3. 5. app/views/todos/_form.html.erb |
109 | 1 | Yuumi Yoshida | |
110 | 15 | Yuumi Yoshida | user.name入力用のコードを追加 |
111 | 1 | Yuumi Yoshida | <pre><code class="color"> |
112 | 14 | Yuumi Yoshida | <%25= form_for(@todo) do |f| %25> |
113 | <%25 if @todo.errors.any? %25> |
||
114 | <div id="error_explanation"> |
||
115 | <h2><%25= pluralize(@todo.errors.count, "error") %25> prohibited this todo from being saved:</h2> |
||
116 | 1 | Yuumi Yoshida | |
117 | 14 | Yuumi Yoshida | <ul> |
118 | <%25 @todo.errors.full_messages.each do |msg| %25> |
||
119 | <li><%25= msg %25></li> |
||
120 | <%25 end %25> |
||
121 | </ul> |
||
122 | </div> |
||
123 | <%25 end %25> |
||
124 | 9 | Yuumi Yoshida | |
125 | 14 | Yuumi Yoshida | <div class="field"> |
126 | <%25= f.label :due %25><br /> |
||
127 | 9 | Yuumi Yoshida | <%25= f.date_select :due %25> |
128 | 14 | Yuumi Yoshida | </div> |
129 | ##(<div class="field"> |
||
130 | <%25= f.label :user_id %25><br /> |
||
131 | <%25= f.select :user_id, User.all.collect {|u| [ u.name, u.id ] } %25> |
||
132 | </div>)## |
||
133 | <div class="field"> |
||
134 | <%25= f.label :task %25><br /> |
||
135 | 1 | Yuumi Yoshida | <%25= f.text_field :task %25> |
136 | 14 | Yuumi Yoshida | </div> |
137 | <div class="field"> |
||
138 | <%25= f.label :memo %25><br /> |
||
139 | 1 | Yuumi Yoshida | <%25= f.text_area :memo %25> |
140 | 14 | Yuumi Yoshida | </div> |
141 | <div class="actions"> |
||
142 | <%25= f.submit %25> |
||
143 | </div> |
||
144 | 1 | Yuumi Yoshida | <%25 end %25> |
145 | </code></pre> |