ScaffoldPlusAnswerThree » 履歴 » バージョン 17
Yuumi Yoshida, 2015-08-03 22:24
1 | 17 | Yuumi Yoshida | # 演習3解答例 |
---|---|---|---|
2 | 7 | Yuumi Yoshida | |
3 | |||
4 | |||
5 | 17 | Yuumi Yoshida | ## 作業手順 |
6 | 7 | Yuumi Yoshida | |
7 | 17 | Yuumi Yoshida | ~~~ |
8 | 12 | Yuumi Yoshida | rails generate model user name:string user_id:integer |
9 | rails generate migration AddUserIdToTodo user_id:integer |
||
10 | 1 | Yuumi Yoshida | rake db:migrate |
11 | 12 | Yuumi Yoshida | rails console # usersデータの作成,todos.user_id更新 |
12 | 16 | Yuumi Yoshida | u = User.new(:id=>1, :name=>'山田') |
13 | 6 | Yuumi Yoshida | u.save |
14 | u = User.new(:id=>2, :name=>'川田') |
||
15 | 1 | Yuumi Yoshida | u.save |
16 | Todo.update_all("user_id=1") |
||
17 | 12 | Yuumi Yoshida | User.all # users の確認 |
18 | Todo.all # todos の確認 |
||
19 | 1 | Yuumi Yoshida | quit |
20 | 17 | Yuumi Yoshida | ~~~ |
21 | 1 | Yuumi Yoshida | |
22 | |||
23 | 17 | Yuumi Yoshida | ## 変更点 |
24 | 1 | Yuumi Yoshida | |
25 | 7 | Yuumi Yoshida | |
26 | |||
27 | 17 | Yuumi Yoshida | ### 1. app/models/user.rb |
28 | 7 | Yuumi Yoshida | |
29 | 1 | Yuumi Yoshida | Todoとの一対多の関連を記述 |
30 | 17 | Yuumi Yoshida | |
31 | ~~~ |
||
32 | 1 | Yuumi Yoshida | class User < ActiveRecord::Base |
33 | 8 | Yuumi Yoshida | ##(has_many :todos)## |
34 | 1 | Yuumi Yoshida | end |
35 | 17 | Yuumi Yoshida | ~~~ |
36 | 1 | Yuumi Yoshida | |
37 | |||
38 | 17 | Yuumi Yoshida | ### 2. app/models/todo.rb |
39 | 1 | Yuumi Yoshida | |
40 | Userとの多対一の関連(従属)を記述 |
||
41 | 17 | Yuumi Yoshida | |
42 | ~~~ |
||
43 | 8 | Yuumi Yoshida | class Todo < ActiveRecord::Base |
44 | ##(belongs_to :user)## |
||
45 | end |
||
46 | 17 | Yuumi Yoshida | ~~~ |
47 | 2 | Yuumi Yoshida | |
48 | 1 | Yuumi Yoshida | |
49 | 17 | Yuumi Yoshida | ### 3. app/views/todos/index.html.erb |
50 | 1 | Yuumi Yoshida | |
51 | user.nameカラム表示用のコードを追加 |
||
52 | 17 | Yuumi Yoshida | |
53 | ~~~ |
||
54 | 1 | Yuumi Yoshida | <h1>Listing todos</h1> |
55 | |||
56 | 8 | Yuumi Yoshida | <table> |
57 | 1 | Yuumi Yoshida | <tr> |
58 | 8 | Yuumi Yoshida | <th>Due</th> |
59 | 1 | Yuumi Yoshida | ##(<th>Name</th>)## |
60 | <th>Task</th> |
||
61 | </tr> |
||
62 | |||
63 | 17 | Yuumi Yoshida | <% for todo in @todos %> |
64 | 14 | Yuumi Yoshida | <tr> |
65 | 17 | Yuumi Yoshida | <td><%= todo.due %></td> |
66 | ##(<td><%= todo.user.name %></td>)## |
||
67 | <td><%= todo.task %></td> |
||
68 | <td><%= link_to 'Show', todo %></td> |
||
69 | <td><%= link_to 'Edit', edit_todo_path(todo) %></td> |
||
70 | <td><%= link_to 'Destroy', todo, :confirm => 'Are you sure?', :method => :delete %></td> |
||
71 | 8 | Yuumi Yoshida | </tr> |
72 | 17 | Yuumi Yoshida | <% end %> |
73 | 1 | Yuumi Yoshida | </table> |
74 | |||
75 | <br /> |
||
76 | |||
77 | 17 | Yuumi Yoshida | <%= link_to 'New todo', new_todo_path %> |
78 | ~~~ |
||
79 | 1 | Yuumi Yoshida | |
80 | |||
81 | 17 | Yuumi Yoshida | ### 4. app/views/todos/show.html.erb |
82 | 1 | Yuumi Yoshida | |
83 | user.nameカラム表示用のコードを追加 |
||
84 | 17 | Yuumi Yoshida | |
85 | ~~~ |
||
86 | 9 | Yuumi Yoshida | <p> |
87 | 1 | Yuumi Yoshida | <b>Due:</b> |
88 | 17 | Yuumi Yoshida | <%= @todo.due %> |
89 | 1 | Yuumi Yoshida | </p> |
90 | |||
91 | <p> |
||
92 | <b>Task:</b> |
||
93 | 17 | Yuumi Yoshida | <%= @todo.task %> |
94 | 1 | Yuumi Yoshida | </p> |
95 | |||
96 | 9 | Yuumi Yoshida | ##(<p> |
97 | 1 | Yuumi Yoshida | <b>Name:</b> |
98 | 17 | Yuumi Yoshida | <%= @todo.user.name %> |
99 | 9 | Yuumi Yoshida | </p>)## |
100 | 14 | Yuumi Yoshida | |
101 | 9 | Yuumi Yoshida | <p> |
102 | <b>Memo:</b><br/> |
||
103 | 17 | Yuumi Yoshida | <%= new_line(@todo.memo) %> |
104 | 9 | Yuumi Yoshida | </p> |
105 | 1 | Yuumi Yoshida | |
106 | |||
107 | 17 | Yuumi Yoshida | <%= link_to 'Edit', edit_todo_path(@todo) %> | |
108 | <%= link_to 'Back', todos_path %> |
||
109 | ~~~ |
||
110 | 1 | Yuumi Yoshida | |
111 | 15 | Yuumi Yoshida | |
112 | 17 | Yuumi Yoshida | ### 5. app/views/todos/_form.html.erb |
113 | 14 | Yuumi Yoshida | |
114 | user.name入力用のコードを追加 |
||
115 | 17 | Yuumi Yoshida | |
116 | ~~~ |
||
117 | <%= form_for(@todo) do |f| %> |
||
118 | <% if @todo.errors.any? %> |
||
119 | 14 | Yuumi Yoshida | <div id="error_explanation"> |
120 | 17 | Yuumi Yoshida | <h2><%= pluralize(@todo.errors.count, "error") %> prohibited this todo from being saved:</h2> |
121 | 14 | Yuumi Yoshida | |
122 | <ul> |
||
123 | 17 | Yuumi Yoshida | <% @todo.errors.full_messages.each do |msg| %> |
124 | <li><%= msg %></li> |
||
125 | <% end %> |
||
126 | 14 | Yuumi Yoshida | </ul> |
127 | </div> |
||
128 | 17 | Yuumi Yoshida | <% end %> |
129 | 14 | Yuumi Yoshida | |
130 | <div class="field"> |
||
131 | 17 | Yuumi Yoshida | <%= f.label :due %><br /> |
132 | <%= f.date_select :due %> |
||
133 | 14 | Yuumi Yoshida | </div> |
134 | ##(<div class="field"> |
||
135 | 17 | Yuumi Yoshida | <%= f.label :user_id %><br /> |
136 | <%= f.select :user_id, User.all.collect {|u| [ u.name, u.id ] } %> |
||
137 | 14 | Yuumi Yoshida | </div>)## |
138 | <div class="field"> |
||
139 | 17 | Yuumi Yoshida | <%= f.label :task %><br /> |
140 | <%= f.text_field :task %> |
||
141 | 14 | Yuumi Yoshida | </div> |
142 | <div class="field"> |
||
143 | 17 | Yuumi Yoshida | <%= f.label :memo %><br /> |
144 | <%= f.text_area :memo %> |
||
145 | 1 | Yuumi Yoshida | </div> |
146 | <div class="actions"> |
||
147 | 17 | Yuumi Yoshida | <%= f.submit %> |
148 | 1 | Yuumi Yoshida | </div> |
149 | 17 | Yuumi Yoshida | <% end %> |
150 | ~~~ |