ScaffoldPlusAnswerThree » 履歴 » バージョン 7
Yuumi Yoshida, 2008-01-13 18:00
1 | 1 | Yuumi Yoshida | |
---|---|---|---|
2 | 7 | Yuumi Yoshida | h1. 演習3解答例 |
3 | |||
4 | |||
5 | |||
6 | h2. 作業手順 |
||
7 | |||
8 | <pre> |
||
9 | 1 | Yuumi Yoshida | ruby script/generate model user name:string user_id:integer |
10 | 7 | Yuumi Yoshida | ruby script/generate migration [[AddUserIdToTodo]] user_id:integer |
11 | 1 | Yuumi Yoshida | rake db:migrate |
12 | 5 | Yuumi Yoshida | ruby script/console # usersデータの作成,todos.user_id更新 |
13 | 6 | Yuumi Yoshida | User.new(:id=>1, :name=>'山田') |
14 | u.save |
||
15 | u = User.new(:id=>2, :name=>'川田') |
||
16 | 1 | Yuumi Yoshida | u.save |
17 | Todo.update_all("user_id=1") |
||
18 | User.find(:all) # users の確認 |
||
19 | Todo.find(:all) # todos の確認 |
||
20 | quit |
||
21 | 7 | Yuumi Yoshida | </pre> |
22 | 1 | Yuumi Yoshida | |
23 | |||
24 | 7 | Yuumi Yoshida | h2. 変更点 |
25 | |||
26 | |||
27 | |||
28 | h3. 1. app/models/user.rb |
||
29 | |||
30 | 1 | Yuumi Yoshida | Todoとの一対多の関連を記述 |
31 | 7 | Yuumi Yoshida | <pre> |
32 | 1 | Yuumi Yoshida | #!html |
33 | 7 | Yuumi Yoshida | <pre class="wiki">class User < [[ActiveRecord]]::Base |
34 | 1 | Yuumi Yoshida | <font color="red">has_many :todos</font> |
35 | end |
||
36 | </pre> |
||
37 | 7 | Yuumi Yoshida | </pre> |
38 | 1 | Yuumi Yoshida | |
39 | 7 | Yuumi Yoshida | |
40 | h3. 2. app/models/todo.rb |
||
41 | |||
42 | 1 | Yuumi Yoshida | Userとの多対一の関連(従属)を記述 |
43 | 7 | Yuumi Yoshida | <pre> |
44 | 1 | Yuumi Yoshida | #!html |
45 | 7 | Yuumi Yoshida | <pre class="wiki">class Todo < [[ActiveRecord]]::Base |
46 | 2 | Yuumi Yoshida | <font color="red">belongs_to :user</font> |
47 | end |
||
48 | </pre> |
||
49 | 7 | Yuumi Yoshida | </pre> |
50 | 2 | Yuumi Yoshida | |
51 | 7 | Yuumi Yoshida | |
52 | h3. 3. app/views/todos/index.html.erb |
||
53 | |||
54 | 1 | Yuumi Yoshida | user.nameカラム表示用のコードを追加 |
55 | 7 | Yuumi Yoshida | <pre> |
56 | 3 | Yuumi Yoshida | #!html |
57 | <pre class="wiki"><h1>Listing todos</h1> |
||
58 | |||
59 | <table> |
||
60 | <tr> |
||
61 | <th>Due</th> |
||
62 | 1 | Yuumi Yoshida | <font color="red"><th>Name</th></font> |
63 | 3 | Yuumi Yoshida | <th>Task</th> |
64 | </tr> |
||
65 | 1 | Yuumi Yoshida | |
66 | <%25 for todo in @todos %25> |
||
67 | <tr> |
||
68 | <td><%25=h todo.due %25></td> |
||
69 | <font color="red"><td><%25=h todo.user.name %25></td></font> |
||
70 | 3 | Yuumi Yoshida | <td><%25=h todo.task %25></td> |
71 | <td><%25= link_to 'Show', todo %25></td> |
||
72 | <td><%25= link_to 'Edit', edit_todo_path(todo) %25></td> |
||
73 | <td><%25= link_to 'Destroy', todo, :confirm => 'Are you sure?', :method => :delete %25></td> |
||
74 | </tr> |
||
75 | <%25 end %25> |
||
76 | </table> |
||
77 | |||
78 | <br /> |
||
79 | 1 | Yuumi Yoshida | |
80 | 3 | Yuumi Yoshida | <%25= link_to 'New todo', new_todo_path %25> |
81 | </pre> |
||
82 | 7 | Yuumi Yoshida | </pre> |
83 | 1 | Yuumi Yoshida | |
84 | 7 | Yuumi Yoshida | |
85 | h3. 4. app/views/todos/show.html.erb |
||
86 | |||
87 | 3 | Yuumi Yoshida | user.nameカラム表示用のコードを追加 |
88 | 7 | Yuumi Yoshida | <pre> |
89 | 3 | Yuumi Yoshida | #!html |
90 | <pre class="wiki"><p> |
||
91 | 1 | Yuumi Yoshida | <b>Due:</b> |
92 | 3 | Yuumi Yoshida | <%25=h @todo.due %25> |
93 | </p> |
||
94 | 1 | Yuumi Yoshida | |
95 | <p> |
||
96 | <b>Task:</b> |
||
97 | <%25=h @todo.task %25> |
||
98 | </p> |
||
99 | 3 | Yuumi Yoshida | |
100 | <font color="red"><p> |
||
101 | 1 | Yuumi Yoshida | <b>Name:</b> |
102 | 3 | Yuumi Yoshida | <%25=h @todo.user.name %25> |
103 | </p></font> |
||
104 | |||
105 | <p> |
||
106 | 1 | Yuumi Yoshida | <b>Memo:</b><br/> |
107 | 3 | Yuumi Yoshida | <%25=new_line(h(@todo.memo)) %25> |
108 | </p> |
||
109 | |||
110 | |||
111 | 1 | Yuumi Yoshida | <%25= link_to 'Edit', edit_todo_path(@todo) %25> | |
112 | <%25= link_to 'Back', todos_path %25> |
||
113 | 3 | Yuumi Yoshida | </pre> |
114 | 7 | Yuumi Yoshida | </pre> |
115 | 3 | Yuumi Yoshida | |
116 | 7 | Yuumi Yoshida | |
117 | h3. 5. app/views/todos/edit.html.erb |
||
118 | |||
119 | 3 | Yuumi Yoshida | user.name入力表示用のコードを追加 |
120 | 7 | Yuumi Yoshida | <pre> |
121 | 3 | Yuumi Yoshida | #!html |
122 | 1 | Yuumi Yoshida | <pre class="wiki"><h1>Editing todo</h1> |
123 | 3 | Yuumi Yoshida | |
124 | <%25= error_messages_for :todo %25> |
||
125 | |||
126 | <%25 form_for(@todo) do |f| %25> |
||
127 | <p> |
||
128 | 1 | Yuumi Yoshida | <b>Due</b><br /> |
129 | 3 | Yuumi Yoshida | <%25= f.date_select :due %25> |
130 | </p> |
||
131 | |||
132 | 1 | Yuumi Yoshida | <p> |
133 | <b>Task</b><br /> |
||
134 | <%25= f.text_field :task %25> |
||
135 | </p> |
||
136 | |||
137 | 3 | Yuumi Yoshida | <font color="red"><p> |
138 | 1 | Yuumi Yoshida | <b>Name</b><br /> |
139 | 3 | Yuumi Yoshida | <%25= f.select :user_id, User.find(:all).collect {|u| [ u.name, u.id ] } %25> |
140 | </p></font> |
||
141 | |||
142 | <p> |
||
143 | 1 | Yuumi Yoshida | <b>Memo</b><br /> |
144 | 3 | Yuumi Yoshida | <%25= f.text_area :memo %25> |
145 | </p> |
||
146 | |||
147 | <p> |
||
148 | 1 | Yuumi Yoshida | <%25= f.submit "Update" %25> |
149 | 3 | Yuumi Yoshida | </p> |
150 | <%25 end %25> |
||
151 | |||
152 | 1 | Yuumi Yoshida | <%25= link_to 'Show', @todo %25> | |
153 | 3 | Yuumi Yoshida | <%25= link_to 'Back', todos_path %25> |
154 | </pre> |
||
155 | 7 | Yuumi Yoshida | </pre> |
156 | 1 | Yuumi Yoshida | |
157 | 7 | Yuumi Yoshida | |
158 | h3. 6. app/views/todos/new.html.erb |
||
159 | |||
160 | 3 | Yuumi Yoshida | user.name入力表示用のコードを追加 |
161 | 7 | Yuumi Yoshida | <pre> |
162 | 1 | Yuumi Yoshida | #!html |
163 | 3 | Yuumi Yoshida | <pre class="wiki"><h1>New todo</h1> |
164 | 1 | Yuumi Yoshida | |
165 | 3 | Yuumi Yoshida | <%25= error_messages_for :todo %25> |
166 | |||
167 | <%25 form_for(@todo) do |f| %25> |
||
168 | <p> |
||
169 | 1 | Yuumi Yoshida | <b>Due</b><br /> |
170 | <%25= f.date_select :due %25> |
||
171 | </p> |
||
172 | |||
173 | 3 | Yuumi Yoshida | <p> |
174 | 1 | Yuumi Yoshida | <b>Task</b><br /> |
175 | 3 | Yuumi Yoshida | <%25= f.text_field :task %25> |
176 | </p> |
||
177 | |||
178 | <font color="red"><p> |
||
179 | 1 | Yuumi Yoshida | <b>Name</b><br /> |
180 | 3 | Yuumi Yoshida | <%25= f.select :user_id, User.find(:all).collect {|u| [ u.name, u.id ] } %25> |
181 | </p></font> |
||
182 | |||
183 | <p> |
||
184 | 1 | Yuumi Yoshida | <b>Memo</b><br /> |
185 | 3 | Yuumi Yoshida | <%25= f.text_area :memo %25> |
186 | </p> |
||
187 | |||
188 | <p> |
||
189 | 1 | Yuumi Yoshida | <%25= f.submit "Create" %25> |
190 | 3 | Yuumi Yoshida | </p> |
191 | <%25 end %25> |
||
192 | |||
193 | <%25= link_to 'Back', todos_path %25> |
||
194 | 1 | Yuumi Yoshida | </pre> |
195 | 7 | Yuumi Yoshida | </pre> |
196 | 3 | Yuumi Yoshida | |
197 | 7 | Yuumi Yoshida | |
198 | h2. ソースコード |
||
199 | |||
200 | 4 | Yuumi Yoshida | |
201 | 解答例のソースコードは http://www.ey-office.com/trac/rails/browser/tags/todo_q3で参照できます。 |