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