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