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