ScaffoldPlusAnswerThree » 履歴 » バージョン 8
Yuumi Yoshida, 2008-07-13 17:40
| 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 | </code></pre> |
||
| 83 | 3 | Yuumi Yoshida | |
| 84 | |||
| 85 | h3. 5. app/views/todos/edit.html.erb |
||
| 86 | 1 | Yuumi Yoshida | |
| 87 | 3 | Yuumi Yoshida | user.name入力表示用のコードを追加 |
| 88 | 8 | Yuumi Yoshida | <pre><code class="color"> |
| 89 | </code></pre> |
||
| 90 | 3 | Yuumi Yoshida | |
| 91 | 1 | Yuumi Yoshida | |
| 92 | 3 | Yuumi Yoshida | |
| 93 | h3. 6. app/views/todos/new.html.erb |
||
| 94 | |||
| 95 | user.name入力表示用のコードを追加 |
||
| 96 | 8 | Yuumi Yoshida | <pre><code class="color"> |
| 97 | </code></pre> |
||
| 98 | 3 | Yuumi Yoshida | |
| 99 | 7 | Yuumi Yoshida | |
| 100 | h2. ソースコード |
||
| 101 | |||
| 102 | 4 | Yuumi Yoshida | |
| 103 | 解答例のソースコードは http://www.ey-office.com/trac/rails/browser/tags/todo_q3で参照できます。 |