ScaffoldPlusAnswerThree » 履歴 » バージョン 2
Yuumi Yoshida, 2008-01-13 16:04
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 | <h1>Listing todos</h1> |
||
36 | |||
37 | <table> |
||
38 | <tr> |
||
39 | <th>Due</th> |
||
40 | <th>Name</th> |
||
41 | <th>Task</th> |
||
42 | </tr> |
||
43 | |||
44 | <%25 for todo in @todos %25> |
||
45 | <tr> |
||
46 | <td><%25=h todo.due %25></td> |
||
47 | <td><%25=h todo.user.name %25></td> |
||
48 | <td><%25=h todo.task %25></td> |
||
49 | <td><%25= link_to 'Show', todo %25></td> |
||
50 | <td><%25= link_to 'Edit', edit_todo_path(todo) %25></td> |
||
51 | <td><%25= link_to 'Destroy', todo, :confirm => 'Are you sure?', :method => :delete %25></td> |
||
52 | </tr> |
||
53 | <%25 end %25> |
||
54 | </table> |
||
55 | |||
56 | <br /> |
||
57 | |||
58 | <%25= link_to 'New todo', new_todo_path %25> |
||
59 | }}} |
||
60 | |||
61 | === 4. app/views/todos/show.html.erb === |
||
62 | user.nameカラム表示用のコードを追加 |
||
63 | {{{ |
||
64 | <p> |
||
65 | <b>Due:</b> |
||
66 | <%25=h @todo.due %25> |
||
67 | </p> |
||
68 | |||
69 | <p> |
||
70 | <b>Task:</b> |
||
71 | <%25=h @todo.task %25> |
||
72 | </p> |
||
73 | |||
74 | <p> |
||
75 | <b>Name:</b> |
||
76 | <%25=h @todo.user.name %25> |
||
77 | </p> |
||
78 | |||
79 | <p> |
||
80 | <b>Memo:</b><br/> |
||
81 | <%25=new_line(h(@todo.memo)) %25> |
||
82 | </p> |
||
83 | |||
84 | |||
85 | <%25= link_to 'Edit', edit_todo_path(@todo) %25> | |
||
86 | <%25= link_to 'Back', todos_path %25> |
||
87 | }}} |
||
88 | |||
89 | === 5. app/views/todos/edit.html.erb === |
||
90 | user.name入力表示用のコードを追加 |
||
91 | {{{ |
||
92 | <h1>Editing todo</h1> |
||
93 | |||
94 | <%25= error_messages_for :todo %25> |
||
95 | |||
96 | <%25 form_for(@todo) do |f| %25> |
||
97 | <p> |
||
98 | <b>Due</b><br /> |
||
99 | <%25= f.date_select :due %25> |
||
100 | </p> |
||
101 | |||
102 | <p> |
||
103 | <b>Task</b><br /> |
||
104 | <%25= f.text_field :task %25> |
||
105 | </p> |
||
106 | |||
107 | <p> |
||
108 | <b>Name</b><br /> |
||
109 | <%25= f.select :user_id, User.find(:all).collect {|u| [ u.name, u.id ] } %25> |
||
110 | </p> |
||
111 | |||
112 | <p> |
||
113 | <b>Memo</b><br /> |
||
114 | <%25= f.text_area :memo %25> |
||
115 | </p> |
||
116 | |||
117 | <p> |
||
118 | <%25= f.submit "Update" %25> |
||
119 | </p> |
||
120 | <%25 end %25> |
||
121 | |||
122 | <%25= link_to 'Show', @todo %25> | |
||
123 | <%25= link_to 'Back', todos_path %25> |
||
124 | }}} |
||
125 | |||
126 | === 6. app/views/todos/new.html.erb === |
||
127 | user.name入力表示用のコードを追加 |
||
128 | {{{ |
||
129 | <h1>New todo</h1> |
||
130 | |||
131 | <%25= error_messages_for :todo %25> |
||
132 | |||
133 | <%25 form_for(@todo) do |f| %25> |
||
134 | <p> |
||
135 | <b>Due</b><br /> |
||
136 | <%25= f.date_select :due %25> |
||
137 | </p> |
||
138 | |||
139 | <p> |
||
140 | <b>Task</b><br /> |
||
141 | <%25= f.text_field :task %25> |
||
142 | </p> |
||
143 | |||
144 | <p> |
||
145 | <b>Name</b><br /> |
||
146 | <%25= f.select :user_id, User.find(:all).collect {|u| [ u.name, u.id ] } %25> |
||
147 | </p> |
||
148 | |||
149 | <p> |
||
150 | <b>Memo</b><br /> |
||
151 | <%25= f.text_area :memo %25> |
||
152 | </p> |
||
153 | |||
154 | <p> |
||
155 | <%25= f.submit "Create" %25> |
||
156 | </p> |
||
157 | <%25 end %25> |
||
158 | |||
159 | <%25= link_to 'Back', todos_path %25> |
||
160 | }}} |