質問編集履歴
1
static_pages_controllerと、users_controllerを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -44,15 +44,255 @@
|
|
44
44
|
|
45
45
|
|
46
46
|
|
47
|
+
###static_pages_controller
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
class StaticPagesController < ApplicationController
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def home
|
56
|
+
|
57
|
+
if logged_in?
|
58
|
+
|
59
|
+
@micropost = current_user.microposts.build
|
60
|
+
|
61
|
+
@feed_items = current_user.feed
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
```
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
###users_controller
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
class UsersController < ApplicationController
|
82
|
+
|
83
|
+
before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :following, :followers]
|
84
|
+
|
85
|
+
before_action :correct_user, only: [:edit, :update]
|
86
|
+
|
87
|
+
before_action :admin_user, only: :destroy
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def index
|
92
|
+
|
93
|
+
@users = User.all.page(params[:page]).per(10)
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
def show
|
100
|
+
|
101
|
+
@user = User.find(params[:id])
|
102
|
+
|
103
|
+
@microposts = @user.microposts
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
def new
|
110
|
+
|
111
|
+
@user = User.new
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
def create
|
118
|
+
|
119
|
+
@user = User.new(user_params)
|
120
|
+
|
121
|
+
if @user.save
|
122
|
+
|
123
|
+
@user.send_activation_email
|
124
|
+
|
125
|
+
flash[:info] = "Please check your email to activate your account."
|
126
|
+
|
127
|
+
redirect_to root_url
|
128
|
+
|
129
|
+
else
|
130
|
+
|
131
|
+
render 'new'
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
def edit
|
140
|
+
|
141
|
+
@user = User.find(params[:id])
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
def update
|
148
|
+
|
149
|
+
@user = User.find(params[:id])
|
150
|
+
|
151
|
+
if @user.update_attributes(user_params)
|
152
|
+
|
153
|
+
flash[:success] = "Profile updated"
|
154
|
+
|
155
|
+
redirect_to @user
|
156
|
+
|
157
|
+
else
|
158
|
+
|
159
|
+
render 'edit'
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
def destroy
|
170
|
+
|
171
|
+
User.find(params[:id]).destroy
|
172
|
+
|
173
|
+
flash[:success] = "User deleted"
|
174
|
+
|
175
|
+
redirect_to users_url
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
def following
|
182
|
+
|
183
|
+
@title = "Following"
|
184
|
+
|
185
|
+
@user = User.find(params[:id])
|
186
|
+
|
187
|
+
@users = @user.following.paginate(page: params[:page])
|
188
|
+
|
189
|
+
render 'show_follow'
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
def followers
|
196
|
+
|
197
|
+
@title = "Followers"
|
198
|
+
|
199
|
+
@user = User.find(params[:id])
|
200
|
+
|
201
|
+
@users = @user.followers.paginate(page: params[:page])
|
202
|
+
|
203
|
+
render 'show_follow'
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
private
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
def user_params
|
212
|
+
|
213
|
+
params.require(:user).permit(:name, :email, :password,
|
214
|
+
|
215
|
+
:password_confirmation, :image)
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
# beforeアクション
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
# 正しいユーザーかどうか確認
|
226
|
+
|
227
|
+
def correct_user
|
228
|
+
|
229
|
+
@user = User.find(params[:id])
|
230
|
+
|
231
|
+
redirect_to(root_url) unless current_user?(@user)
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
# 管理者かどうか確認
|
238
|
+
|
239
|
+
def admin_user
|
240
|
+
|
241
|
+
redirect_to(root_url) unless current_user.admin?
|
242
|
+
|
243
|
+
end
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
|
250
|
+
|
47
251
|
###ホーム画面
|
48
252
|
|
49
253
|
```
|
50
254
|
|
255
|
+
<% if logged_in? %>
|
256
|
+
|
257
|
+
<div class="row">
|
258
|
+
|
259
|
+
<aside class="col-md-4">
|
260
|
+
|
51
|
-
<section class="user_info">
|
261
|
+
<section class="user_info">
|
52
|
-
|
262
|
+
|
53
|
-
<%= render 'shared/user_info' %>
|
263
|
+
<%= render 'shared/user_info' %>
|
54
|
-
|
264
|
+
|
55
|
-
</section>
|
265
|
+
</section>
|
266
|
+
|
267
|
+
<section class="stats">
|
268
|
+
|
269
|
+
<%= render 'shared/stats' %>
|
270
|
+
|
271
|
+
</section>
|
272
|
+
|
273
|
+
<section class="micropost_form">
|
274
|
+
|
275
|
+
<%= render 'shared/micropost_form' %>
|
276
|
+
|
277
|
+
</section>
|
278
|
+
|
279
|
+
</aside>
|
280
|
+
|
281
|
+
<div class="col-md-8">
|
282
|
+
|
283
|
+
<h3>Micropost Feed</h3>
|
284
|
+
|
285
|
+
<%= render 'shared/feed' %>
|
286
|
+
|
287
|
+
</div>
|
288
|
+
|
289
|
+
</div>
|
290
|
+
|
291
|
+
<% else %>
|
292
|
+
|
293
|
+
,,,
|
294
|
+
|
295
|
+
<%end%>
|
56
296
|
|
57
297
|
```
|
58
298
|
|