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