質問編集履歴
3
マイグレーションファイルと、テスト環境のコンソールにおける表示を追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -355,3 +355,51 @@
|
|
355
355
|
end
|
356
356
|
|
357
357
|
```
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
また、以下のマイグレーションを実行しており、コンソールでは以下の表示を得られています。
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
```ここに言語を入力
|
366
|
+
|
367
|
+
class AddPictureToMicroposts < ActiveRecord::Migration[5.1]
|
368
|
+
|
369
|
+
def change
|
370
|
+
|
371
|
+
add_column :microposts, :picture, :string
|
372
|
+
|
373
|
+
end
|
374
|
+
|
375
|
+
end
|
376
|
+
|
377
|
+
```
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
```ここに言語を入力
|
382
|
+
|
383
|
+
ec2-user:~/environment/ap (master) $ rails c test
|
384
|
+
|
385
|
+
Running via Spring preloader in process 4052
|
386
|
+
|
387
|
+
Loading test environment (Rails 5.1.4)
|
388
|
+
|
389
|
+
2.6.3 :001 > micropost = Micropost.first
|
390
|
+
|
391
|
+
Micropost Load (1.9ms) SELECT "microposts".* FROM "microposts" ORDER BY "microposts"."created_at" DESC LIMIT ? [["LIMIT", 1]]
|
392
|
+
|
393
|
+
=> #<Micropost id: 941832919, content: "Writing a short test", user_id: 762146111, created_at: "2019-10-06 04:14:25", updated_at: "2019-10-06 04:14:25", picture: nil>
|
394
|
+
|
395
|
+
2.6.3 :002 > user = User.first
|
396
|
+
|
397
|
+
User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
|
398
|
+
|
399
|
+
=> #<User id: 14035331, name: "User 19", email: "user-19@example.com", created_at: "2019-10-06 04:14:25", updated_at: "2019-10-06 04:14:25", password_digest: "$2a$04$n8A5cjRLPSUv./2tCFaC4eKBRXxDmaGryv/FJn6AXMG...", remember_digest: nil, admin: false, activation_digest: nil, activated: true, activated_at: "2019-10-06 04:14:25", reset_digest: nil, reset_sent_at: nil>
|
400
|
+
|
401
|
+
2.6.3 :003 >
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
```
|
2
user_controller.rbファイルについて、全文を追加しました。 また、エラーが起こったテストのコードについても追加で記入しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -32,9 +32,69 @@
|
|
32
32
|
|
33
33
|
```ここに言語を入力
|
34
34
|
|
35
|
+
|
36
|
+
|
37
|
+
require 'test_helper'
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
class UsersIndexTest < ActionDispatch::IntegrationTest
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
def setup
|
46
|
+
|
47
|
+
@admin = users(:michael)
|
48
|
+
|
49
|
+
@non_admin = users(:archer)
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
test 'index as admin including pagination and delete links' do
|
56
|
+
|
57
|
+
log_in_as(@admin)
|
58
|
+
|
59
|
+
get users_path
|
60
|
+
|
61
|
+
assert_template 'users/index'
|
62
|
+
|
63
|
+
assert_select 'div.pagination'
|
64
|
+
|
65
|
+
first_page_of_users = User.paginate(page: 1)
|
66
|
+
|
67
|
+
first_page_of_users.each do |user|
|
68
|
+
|
69
|
+
assert_select 'a[href=?]', user_path(user), text: user.name
|
70
|
+
|
71
|
+
unless user == @admin
|
72
|
+
|
73
|
+
assert_select 'a[href=?]', user_path(user), text: 'delete'
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
assert_difference 'User.count', -1 do
|
80
|
+
|
81
|
+
delete user_path(@non_admin)
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
```ここに言語を入力
|
94
|
+
|
35
95
|
class UsersController < ApplicationController
|
36
96
|
|
37
|
-
before_action :logged_in_user, only: [:edit, :update, :index, :destroy]
|
97
|
+
before_action :logged_in_user, only: [:edit, :update, :index, :destroy, :following, :followers]
|
38
98
|
|
39
99
|
before_action :correct_user, only: [:edit, :update]
|
40
100
|
|
@@ -42,6 +102,84 @@
|
|
42
102
|
|
43
103
|
|
44
104
|
|
105
|
+
def show
|
106
|
+
|
107
|
+
@user = User.find(params[:id])
|
108
|
+
|
109
|
+
@microposts = @user.microposts.paginate(page: params[:page])
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
redirect_to root_url and return unless @user.activated == true
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
def index
|
120
|
+
|
121
|
+
@users = User.where(activated: true).paginate(page: params[:page])
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def new
|
130
|
+
|
131
|
+
@user = User.new
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
def create
|
138
|
+
|
139
|
+
@user = User.new(user_params)
|
140
|
+
|
141
|
+
if @user.save
|
142
|
+
|
143
|
+
@user.send_activation_email
|
144
|
+
|
145
|
+
flash[:info] = 'Please check your emial to activate your account'
|
146
|
+
|
147
|
+
redirect_to root_url
|
148
|
+
|
149
|
+
else
|
150
|
+
|
151
|
+
render 'new'
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
def edit
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
def update
|
166
|
+
|
167
|
+
if @user.update_attributes(user_params)
|
168
|
+
|
169
|
+
flash[:success] = "Profile updated"
|
170
|
+
|
171
|
+
redirect_to @user
|
172
|
+
|
173
|
+
else
|
174
|
+
|
175
|
+
render 'edit'
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
|
182
|
+
|
45
183
|
def destroy
|
46
184
|
|
47
185
|
User.find(params[:id]).destroy
|
@@ -52,9 +190,39 @@
|
|
52
190
|
|
53
191
|
end
|
54
192
|
|
193
|
+
|
194
|
+
|
55
|
-
|
195
|
+
def following
|
196
|
+
|
56
|
-
|
197
|
+
@title = 'Following'
|
198
|
+
|
199
|
+
@user = User.find(params[:id])
|
200
|
+
|
201
|
+
@users = @user.following.paginate(page: params[:page])
|
202
|
+
|
203
|
+
render 'show_follow'
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
def followers
|
210
|
+
|
211
|
+
@title = 'Followers'
|
212
|
+
|
213
|
+
@user = User.find(params[:id])
|
214
|
+
|
215
|
+
@users = @user.followers.paginate(page: params[:page])
|
216
|
+
|
217
|
+
render 'show_follow'
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
57
|
-
private
|
225
|
+
private
|
58
226
|
|
59
227
|
def user_params
|
60
228
|
|
@@ -80,6 +248,8 @@
|
|
80
248
|
|
81
249
|
end
|
82
250
|
|
251
|
+
|
252
|
+
|
83
253
|
end
|
84
254
|
|
85
255
|
```
|
1
マイグレーションファイルのコードを記入し忘れていました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -160,6 +160,28 @@
|
|
160
160
|
|
161
161
|
```ここに言語を入力
|
162
162
|
|
163
|
+
class CreateMicroposts < ActiveRecord::Migration[5.0]
|
163
164
|
|
165
|
+
def change
|
166
|
+
|
167
|
+
create_table :microposts do |t|
|
168
|
+
|
169
|
+
t.text :content
|
170
|
+
|
171
|
+
t.references :user, foreign_key: true
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
t.timestamps
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
add_index :microposts, [:user_id, :created_at]
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
164
186
|
|
165
187
|
```
|